xilinx_hwicap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. /*****************************************************************************
  2. *
  3. * Author: Xilinx, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"
  11. * AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND
  12. * SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,
  13. * OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,
  14. * APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION
  15. * THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,
  16. * AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE
  17. * FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY
  18. * WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE
  19. * IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR
  20. * REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF
  21. * INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE.
  23. *
  24. * Xilinx products are not intended for use in life support appliances,
  25. * devices, or systems. Use in such applications is expressly prohibited.
  26. *
  27. * (c) Copyright 2002 Xilinx Inc., Systems Engineering Group
  28. * (c) Copyright 2004 Xilinx Inc., Systems Engineering Group
  29. * (c) Copyright 2007-2008 Xilinx Inc.
  30. * All rights reserved.
  31. *
  32. * You should have received a copy of the GNU General Public License along
  33. * with this program; if not, write to the Free Software Foundation, Inc.,
  34. * 675 Mass Ave, Cambridge, MA 02139, USA.
  35. *
  36. *****************************************************************************/
  37. /*
  38. * This is the code behind /dev/xilinx_icap -- it allows a user-space
  39. * application to use the Xilinx ICAP subsystem.
  40. *
  41. * The following operations are possible:
  42. *
  43. * open open the port and initialize for access.
  44. * release release port
  45. * write Write a bitstream to the configuration processor.
  46. * read Read a data stream from the configuration processor.
  47. *
  48. * After being opened, the port is initialized and accessed to avoid a
  49. * corrupted first read which may occur with some hardware. The port
  50. * is left in a desynched state, requiring that a synch sequence be
  51. * transmitted before any valid configuration data. A user will have
  52. * exclusive access to the device while it remains open, and the state
  53. * of the ICAP cannot be guaranteed after the device is closed. Note
  54. * that a complete reset of the core and the state of the ICAP cannot
  55. * be performed on many versions of the cores, hence users of this
  56. * device should avoid making inconsistent accesses to the device. In
  57. * particular, accessing the read interface, without first generating
  58. * a write containing a readback packet can leave the ICAP in an
  59. * inaccessible state.
  60. *
  61. * Note that in order to use the read interface, it is first necessary
  62. * to write a request packet to the write interface. i.e., it is not
  63. * possible to simply readback the bitstream (or any configuration
  64. * bits) from a device without specifically requesting them first.
  65. * The code to craft such packets is intended to be part of the
  66. * user-space application code that uses this device. The simplest
  67. * way to use this interface is simply:
  68. *
  69. * cp foo.bit /dev/xilinx_icap
  70. *
  71. * Note that unless foo.bit is an appropriately constructed partial
  72. * bitstream, this has a high likelyhood of overwriting the design
  73. * currently programmed in the FPGA.
  74. */
  75. #include <linux/version.h>
  76. #include <linux/module.h>
  77. #include <linux/kernel.h>
  78. #include <linux/types.h>
  79. #include <linux/ioport.h>
  80. #include <linux/interrupt.h>
  81. #include <linux/fcntl.h>
  82. #include <linux/init.h>
  83. #include <linux/poll.h>
  84. #include <linux/proc_fs.h>
  85. #include <linux/mutex.h>
  86. #include <linux/sysctl.h>
  87. #include <linux/version.h>
  88. #include <linux/fs.h>
  89. #include <linux/cdev.h>
  90. #include <linux/platform_device.h>
  91. #include <asm/io.h>
  92. #include <asm/uaccess.h>
  93. #include <asm/system.h>
  94. #ifdef CONFIG_OF
  95. /* For open firmware. */
  96. #include <linux/of_device.h>
  97. #include <linux/of_platform.h>
  98. #endif
  99. #include "xilinx_hwicap.h"
  100. #include "buffer_icap.h"
  101. #include "fifo_icap.h"
  102. #define DRIVER_NAME "xilinx_icap"
  103. #define HWICAP_REGS (0x10000)
  104. /* dynamically allocate device number */
  105. static int xhwicap_major;
  106. static int xhwicap_minor;
  107. #define HWICAP_DEVICES 1
  108. module_param(xhwicap_major, int, S_IRUGO);
  109. module_param(xhwicap_minor, int, S_IRUGO);
  110. /* An array, which is set to true when the device is registered. */
  111. static bool probed_devices[HWICAP_DEVICES];
  112. static struct mutex icap_sem;
  113. static struct class *icap_class;
  114. #define UNIMPLEMENTED 0xFFFF
  115. static const struct config_registers v2_config_registers = {
  116. .CRC = 0,
  117. .FAR = 1,
  118. .FDRI = 2,
  119. .FDRO = 3,
  120. .CMD = 4,
  121. .CTL = 5,
  122. .MASK = 6,
  123. .STAT = 7,
  124. .LOUT = 8,
  125. .COR = 9,
  126. .MFWR = 10,
  127. .FLR = 11,
  128. .KEY = 12,
  129. .CBC = 13,
  130. .IDCODE = 14,
  131. .AXSS = UNIMPLEMENTED,
  132. .C0R_1 = UNIMPLEMENTED,
  133. .CSOB = UNIMPLEMENTED,
  134. .WBSTAR = UNIMPLEMENTED,
  135. .TIMER = UNIMPLEMENTED,
  136. .BOOTSTS = UNIMPLEMENTED,
  137. .CTL_1 = UNIMPLEMENTED,
  138. };
  139. static const struct config_registers v4_config_registers = {
  140. .CRC = 0,
  141. .FAR = 1,
  142. .FDRI = 2,
  143. .FDRO = 3,
  144. .CMD = 4,
  145. .CTL = 5,
  146. .MASK = 6,
  147. .STAT = 7,
  148. .LOUT = 8,
  149. .COR = 9,
  150. .MFWR = 10,
  151. .FLR = UNIMPLEMENTED,
  152. .KEY = UNIMPLEMENTED,
  153. .CBC = 11,
  154. .IDCODE = 12,
  155. .AXSS = 13,
  156. .C0R_1 = UNIMPLEMENTED,
  157. .CSOB = UNIMPLEMENTED,
  158. .WBSTAR = UNIMPLEMENTED,
  159. .TIMER = UNIMPLEMENTED,
  160. .BOOTSTS = UNIMPLEMENTED,
  161. .CTL_1 = UNIMPLEMENTED,
  162. };
  163. static const struct config_registers v5_config_registers = {
  164. .CRC = 0,
  165. .FAR = 1,
  166. .FDRI = 2,
  167. .FDRO = 3,
  168. .CMD = 4,
  169. .CTL = 5,
  170. .MASK = 6,
  171. .STAT = 7,
  172. .LOUT = 8,
  173. .COR = 9,
  174. .MFWR = 10,
  175. .FLR = UNIMPLEMENTED,
  176. .KEY = UNIMPLEMENTED,
  177. .CBC = 11,
  178. .IDCODE = 12,
  179. .AXSS = 13,
  180. .C0R_1 = 14,
  181. .CSOB = 15,
  182. .WBSTAR = 16,
  183. .TIMER = 17,
  184. .BOOTSTS = 18,
  185. .CTL_1 = 19,
  186. };
  187. /**
  188. * hwicap_command_desync - Send a DESYNC command to the ICAP port.
  189. * @drvdata: a pointer to the drvdata.
  190. *
  191. * This command desynchronizes the ICAP After this command, a
  192. * bitstream containing a NULL packet, followed by a SYNCH packet is
  193. * required before the ICAP will recognize commands.
  194. */
  195. static int hwicap_command_desync(struct hwicap_drvdata *drvdata)
  196. {
  197. u32 buffer[4];
  198. u32 index = 0;
  199. /*
  200. * Create the data to be written to the ICAP.
  201. */
  202. buffer[index++] = hwicap_type_1_write(drvdata->config_regs->CMD) | 1;
  203. buffer[index++] = XHI_CMD_DESYNCH;
  204. buffer[index++] = XHI_NOOP_PACKET;
  205. buffer[index++] = XHI_NOOP_PACKET;
  206. /*
  207. * Write the data to the FIFO and intiate the transfer of data present
  208. * in the FIFO to the ICAP device.
  209. */
  210. return drvdata->config->set_configuration(drvdata,
  211. &buffer[0], index);
  212. }
  213. /**
  214. * hwicap_get_configuration_register - Query a configuration register.
  215. * @drvdata: a pointer to the drvdata.
  216. * @reg: a constant which represents the configuration
  217. * register value to be returned.
  218. * Examples: XHI_IDCODE, XHI_FLR.
  219. * @reg_data: returns the value of the register.
  220. *
  221. * Sends a query packet to the ICAP and then receives the response.
  222. * The icap is left in Synched state.
  223. */
  224. static int hwicap_get_configuration_register(struct hwicap_drvdata *drvdata,
  225. u32 reg, u32 *reg_data)
  226. {
  227. int status;
  228. u32 buffer[6];
  229. u32 index = 0;
  230. /*
  231. * Create the data to be written to the ICAP.
  232. */
  233. buffer[index++] = XHI_DUMMY_PACKET;
  234. buffer[index++] = XHI_SYNC_PACKET;
  235. buffer[index++] = XHI_NOOP_PACKET;
  236. buffer[index++] = hwicap_type_1_read(reg) | 1;
  237. buffer[index++] = XHI_NOOP_PACKET;
  238. buffer[index++] = XHI_NOOP_PACKET;
  239. /*
  240. * Write the data to the FIFO and intiate the transfer of data present
  241. * in the FIFO to the ICAP device.
  242. */
  243. status = drvdata->config->set_configuration(drvdata,
  244. &buffer[0], index);
  245. if (status)
  246. return status;
  247. /*
  248. * Read the configuration register
  249. */
  250. status = drvdata->config->get_configuration(drvdata, reg_data, 1);
  251. if (status)
  252. return status;
  253. return 0;
  254. }
  255. static int hwicap_initialize_hwicap(struct hwicap_drvdata *drvdata)
  256. {
  257. int status;
  258. u32 idcode;
  259. dev_dbg(drvdata->dev, "initializing\n");
  260. /* Abort any current transaction, to make sure we have the
  261. * ICAP in a good state. */
  262. dev_dbg(drvdata->dev, "Reset...\n");
  263. drvdata->config->reset(drvdata);
  264. dev_dbg(drvdata->dev, "Desync...\n");
  265. status = hwicap_command_desync(drvdata);
  266. if (status)
  267. return status;
  268. /* Attempt to read the IDCODE from ICAP. This
  269. * may not be returned correctly, due to the design of the
  270. * hardware.
  271. */
  272. dev_dbg(drvdata->dev, "Reading IDCODE...\n");
  273. status = hwicap_get_configuration_register(
  274. drvdata, drvdata->config_regs->IDCODE, &idcode);
  275. dev_dbg(drvdata->dev, "IDCODE = %x\n", idcode);
  276. if (status)
  277. return status;
  278. dev_dbg(drvdata->dev, "Desync...\n");
  279. status = hwicap_command_desync(drvdata);
  280. if (status)
  281. return status;
  282. return 0;
  283. }
  284. static ssize_t
  285. hwicap_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  286. {
  287. struct hwicap_drvdata *drvdata = file->private_data;
  288. ssize_t bytes_to_read = 0;
  289. u32 *kbuf;
  290. u32 words;
  291. u32 bytes_remaining;
  292. int status;
  293. status = mutex_lock_interruptible(&drvdata->sem);
  294. if (status)
  295. return status;
  296. if (drvdata->read_buffer_in_use) {
  297. /* If there are leftover bytes in the buffer, just */
  298. /* return them and don't try to read more from the */
  299. /* ICAP device. */
  300. bytes_to_read =
  301. (count < drvdata->read_buffer_in_use) ? count :
  302. drvdata->read_buffer_in_use;
  303. /* Return the data currently in the read buffer. */
  304. if (copy_to_user(buf, drvdata->read_buffer, bytes_to_read)) {
  305. status = -EFAULT;
  306. goto error;
  307. }
  308. drvdata->read_buffer_in_use -= bytes_to_read;
  309. memmove(drvdata->read_buffer,
  310. drvdata->read_buffer + bytes_to_read,
  311. 4 - bytes_to_read);
  312. } else {
  313. /* Get new data from the ICAP, and return was was requested. */
  314. kbuf = (u32 *) get_zeroed_page(GFP_KERNEL);
  315. if (!kbuf) {
  316. status = -ENOMEM;
  317. goto error;
  318. }
  319. /* The ICAP device is only able to read complete */
  320. /* words. If a number of bytes that do not correspond */
  321. /* to complete words is requested, then we read enough */
  322. /* words to get the required number of bytes, and then */
  323. /* save the remaining bytes for the next read. */
  324. /* Determine the number of words to read, rounding up */
  325. /* if necessary. */
  326. words = ((count + 3) >> 2);
  327. bytes_to_read = words << 2;
  328. if (bytes_to_read > PAGE_SIZE)
  329. bytes_to_read = PAGE_SIZE;
  330. /* Ensure we only read a complete number of words. */
  331. bytes_remaining = bytes_to_read & 3;
  332. bytes_to_read &= ~3;
  333. words = bytes_to_read >> 2;
  334. status = drvdata->config->get_configuration(drvdata,
  335. kbuf, words);
  336. /* If we didn't read correctly, then bail out. */
  337. if (status) {
  338. free_page((unsigned long)kbuf);
  339. goto error;
  340. }
  341. /* If we fail to return the data to the user, then bail out. */
  342. if (copy_to_user(buf, kbuf, bytes_to_read)) {
  343. free_page((unsigned long)kbuf);
  344. status = -EFAULT;
  345. goto error;
  346. }
  347. memcpy(drvdata->read_buffer,
  348. kbuf,
  349. bytes_remaining);
  350. drvdata->read_buffer_in_use = bytes_remaining;
  351. free_page((unsigned long)kbuf);
  352. }
  353. status = bytes_to_read;
  354. error:
  355. mutex_unlock(&drvdata->sem);
  356. return status;
  357. }
  358. static ssize_t
  359. hwicap_write(struct file *file, const char __user *buf,
  360. size_t count, loff_t *ppos)
  361. {
  362. struct hwicap_drvdata *drvdata = file->private_data;
  363. ssize_t written = 0;
  364. ssize_t left = count;
  365. u32 *kbuf;
  366. ssize_t len;
  367. ssize_t status;
  368. status = mutex_lock_interruptible(&drvdata->sem);
  369. if (status)
  370. return status;
  371. left += drvdata->write_buffer_in_use;
  372. /* Only write multiples of 4 bytes. */
  373. if (left < 4) {
  374. status = 0;
  375. goto error;
  376. }
  377. kbuf = (u32 *) __get_free_page(GFP_KERNEL);
  378. if (!kbuf) {
  379. status = -ENOMEM;
  380. goto error;
  381. }
  382. while (left > 3) {
  383. /* only write multiples of 4 bytes, so there might */
  384. /* be as many as 3 bytes left (at the end). */
  385. len = left;
  386. if (len > PAGE_SIZE)
  387. len = PAGE_SIZE;
  388. len &= ~3;
  389. if (drvdata->write_buffer_in_use) {
  390. memcpy(kbuf, drvdata->write_buffer,
  391. drvdata->write_buffer_in_use);
  392. if (copy_from_user(
  393. (((char *)kbuf) + drvdata->write_buffer_in_use),
  394. buf + written,
  395. len - (drvdata->write_buffer_in_use))) {
  396. free_page((unsigned long)kbuf);
  397. status = -EFAULT;
  398. goto error;
  399. }
  400. } else {
  401. if (copy_from_user(kbuf, buf + written, len)) {
  402. free_page((unsigned long)kbuf);
  403. status = -EFAULT;
  404. goto error;
  405. }
  406. }
  407. status = drvdata->config->set_configuration(drvdata,
  408. kbuf, len >> 2);
  409. if (status) {
  410. free_page((unsigned long)kbuf);
  411. status = -EFAULT;
  412. goto error;
  413. }
  414. if (drvdata->write_buffer_in_use) {
  415. len -= drvdata->write_buffer_in_use;
  416. left -= drvdata->write_buffer_in_use;
  417. drvdata->write_buffer_in_use = 0;
  418. }
  419. written += len;
  420. left -= len;
  421. }
  422. if ((left > 0) && (left < 4)) {
  423. if (!copy_from_user(drvdata->write_buffer,
  424. buf + written, left)) {
  425. drvdata->write_buffer_in_use = left;
  426. written += left;
  427. left = 0;
  428. }
  429. }
  430. free_page((unsigned long)kbuf);
  431. status = written;
  432. error:
  433. mutex_unlock(&drvdata->sem);
  434. return status;
  435. }
  436. static int hwicap_open(struct inode *inode, struct file *file)
  437. {
  438. struct hwicap_drvdata *drvdata;
  439. int status;
  440. drvdata = container_of(inode->i_cdev, struct hwicap_drvdata, cdev);
  441. status = mutex_lock_interruptible(&drvdata->sem);
  442. if (status)
  443. return status;
  444. if (drvdata->is_open) {
  445. status = -EBUSY;
  446. goto error;
  447. }
  448. status = hwicap_initialize_hwicap(drvdata);
  449. if (status) {
  450. dev_err(drvdata->dev, "Failed to open file");
  451. goto error;
  452. }
  453. file->private_data = drvdata;
  454. drvdata->write_buffer_in_use = 0;
  455. drvdata->read_buffer_in_use = 0;
  456. drvdata->is_open = 1;
  457. error:
  458. mutex_unlock(&drvdata->sem);
  459. return status;
  460. }
  461. static int hwicap_release(struct inode *inode, struct file *file)
  462. {
  463. struct hwicap_drvdata *drvdata = file->private_data;
  464. int i;
  465. int status = 0;
  466. mutex_lock(&drvdata->sem);
  467. if (drvdata->write_buffer_in_use) {
  468. /* Flush write buffer. */
  469. for (i = drvdata->write_buffer_in_use; i < 4; i++)
  470. drvdata->write_buffer[i] = 0;
  471. status = drvdata->config->set_configuration(drvdata,
  472. (u32 *) drvdata->write_buffer, 1);
  473. if (status)
  474. goto error;
  475. }
  476. status = hwicap_command_desync(drvdata);
  477. if (status)
  478. goto error;
  479. error:
  480. drvdata->is_open = 0;
  481. mutex_unlock(&drvdata->sem);
  482. return status;
  483. }
  484. static struct file_operations hwicap_fops = {
  485. .owner = THIS_MODULE,
  486. .write = hwicap_write,
  487. .read = hwicap_read,
  488. .open = hwicap_open,
  489. .release = hwicap_release,
  490. };
  491. static int __devinit hwicap_setup(struct device *dev, int id,
  492. const struct resource *regs_res,
  493. const struct hwicap_driver_config *config,
  494. const struct config_registers *config_regs)
  495. {
  496. dev_t devt;
  497. struct hwicap_drvdata *drvdata = NULL;
  498. int retval = 0;
  499. dev_info(dev, "Xilinx icap port driver\n");
  500. mutex_lock(&icap_sem);
  501. if (id < 0) {
  502. for (id = 0; id < HWICAP_DEVICES; id++)
  503. if (!probed_devices[id])
  504. break;
  505. }
  506. if (id < 0 || id >= HWICAP_DEVICES) {
  507. mutex_unlock(&icap_sem);
  508. dev_err(dev, "%s%i too large\n", DRIVER_NAME, id);
  509. return -EINVAL;
  510. }
  511. if (probed_devices[id]) {
  512. mutex_unlock(&icap_sem);
  513. dev_err(dev, "cannot assign to %s%i; it is already in use\n",
  514. DRIVER_NAME, id);
  515. return -EBUSY;
  516. }
  517. probed_devices[id] = 1;
  518. mutex_unlock(&icap_sem);
  519. devt = MKDEV(xhwicap_major, xhwicap_minor + id);
  520. drvdata = kzalloc(sizeof(struct hwicap_drvdata), GFP_KERNEL);
  521. if (!drvdata) {
  522. dev_err(dev, "Couldn't allocate device private record\n");
  523. retval = -ENOMEM;
  524. goto failed0;
  525. }
  526. dev_set_drvdata(dev, (void *)drvdata);
  527. if (!regs_res) {
  528. dev_err(dev, "Couldn't get registers resource\n");
  529. retval = -EFAULT;
  530. goto failed1;
  531. }
  532. drvdata->mem_start = regs_res->start;
  533. drvdata->mem_end = regs_res->end;
  534. drvdata->mem_size = regs_res->end - regs_res->start + 1;
  535. if (!request_mem_region(drvdata->mem_start,
  536. drvdata->mem_size, DRIVER_NAME)) {
  537. dev_err(dev, "Couldn't lock memory region at %p\n",
  538. (void *)regs_res->start);
  539. retval = -EBUSY;
  540. goto failed1;
  541. }
  542. drvdata->devt = devt;
  543. drvdata->dev = dev;
  544. drvdata->base_address = ioremap(drvdata->mem_start, drvdata->mem_size);
  545. if (!drvdata->base_address) {
  546. dev_err(dev, "ioremap() failed\n");
  547. goto failed2;
  548. }
  549. drvdata->config = config;
  550. drvdata->config_regs = config_regs;
  551. mutex_init(&drvdata->sem);
  552. drvdata->is_open = 0;
  553. dev_info(dev, "ioremap %lx to %p with size %x\n",
  554. (unsigned long int)drvdata->mem_start,
  555. drvdata->base_address, drvdata->mem_size);
  556. cdev_init(&drvdata->cdev, &hwicap_fops);
  557. drvdata->cdev.owner = THIS_MODULE;
  558. retval = cdev_add(&drvdata->cdev, devt, 1);
  559. if (retval) {
  560. dev_err(dev, "cdev_add() failed\n");
  561. goto failed3;
  562. }
  563. /* devfs_mk_cdev(devt, S_IFCHR|S_IRUGO|S_IWUGO, DRIVER_NAME); */
  564. device_create(icap_class, dev, devt, "%s%d", DRIVER_NAME, id);
  565. return 0; /* success */
  566. failed3:
  567. iounmap(drvdata->base_address);
  568. failed2:
  569. release_mem_region(regs_res->start, drvdata->mem_size);
  570. failed1:
  571. kfree(drvdata);
  572. failed0:
  573. mutex_lock(&icap_sem);
  574. probed_devices[id] = 0;
  575. mutex_unlock(&icap_sem);
  576. return retval;
  577. }
  578. static struct hwicap_driver_config buffer_icap_config = {
  579. .get_configuration = buffer_icap_get_configuration,
  580. .set_configuration = buffer_icap_set_configuration,
  581. .reset = buffer_icap_reset,
  582. };
  583. static struct hwicap_driver_config fifo_icap_config = {
  584. .get_configuration = fifo_icap_get_configuration,
  585. .set_configuration = fifo_icap_set_configuration,
  586. .reset = fifo_icap_reset,
  587. };
  588. static int __devexit hwicap_remove(struct device *dev)
  589. {
  590. struct hwicap_drvdata *drvdata;
  591. drvdata = (struct hwicap_drvdata *)dev_get_drvdata(dev);
  592. if (!drvdata)
  593. return 0;
  594. device_destroy(icap_class, drvdata->devt);
  595. cdev_del(&drvdata->cdev);
  596. iounmap(drvdata->base_address);
  597. release_mem_region(drvdata->mem_start, drvdata->mem_size);
  598. kfree(drvdata);
  599. dev_set_drvdata(dev, NULL);
  600. mutex_lock(&icap_sem);
  601. probed_devices[MINOR(dev->devt)-xhwicap_minor] = 0;
  602. mutex_unlock(&icap_sem);
  603. return 0; /* success */
  604. }
  605. static int __devinit hwicap_drv_probe(struct platform_device *pdev)
  606. {
  607. struct resource *res;
  608. const struct config_registers *regs;
  609. const char *family;
  610. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  611. if (!res)
  612. return -ENODEV;
  613. /* It's most likely that we're using V4, if the family is not
  614. specified */
  615. regs = &v4_config_registers;
  616. family = pdev->dev.platform_data;
  617. if (family) {
  618. if (!strcmp(family, "virtex2p")) {
  619. regs = &v2_config_registers;
  620. } else if (!strcmp(family, "virtex4")) {
  621. regs = &v4_config_registers;
  622. } else if (!strcmp(family, "virtex5")) {
  623. regs = &v5_config_registers;
  624. }
  625. }
  626. return hwicap_setup(&pdev->dev, pdev->id, res,
  627. &buffer_icap_config, regs);
  628. }
  629. static int __devexit hwicap_drv_remove(struct platform_device *pdev)
  630. {
  631. return hwicap_remove(&pdev->dev);
  632. }
  633. static struct platform_driver hwicap_platform_driver = {
  634. .probe = hwicap_drv_probe,
  635. .remove = hwicap_drv_remove,
  636. .driver = {
  637. .owner = THIS_MODULE,
  638. .name = DRIVER_NAME,
  639. },
  640. };
  641. /* ---------------------------------------------------------------------
  642. * OF bus binding
  643. */
  644. #if defined(CONFIG_OF)
  645. static int __devinit
  646. hwicap_of_probe(struct of_device *op, const struct of_device_id *match)
  647. {
  648. struct resource res;
  649. const unsigned int *id;
  650. const char *family;
  651. int rc;
  652. const struct hwicap_driver_config *config = match->data;
  653. const struct config_registers *regs;
  654. dev_dbg(&op->dev, "hwicap_of_probe(%p, %p)\n", op, match);
  655. rc = of_address_to_resource(op->node, 0, &res);
  656. if (rc) {
  657. dev_err(&op->dev, "invalid address\n");
  658. return rc;
  659. }
  660. id = of_get_property(op->node, "port-number", NULL);
  661. /* It's most likely that we're using V4, if the family is not
  662. specified */
  663. regs = &v4_config_registers;
  664. family = of_get_property(op->node, "xlnx,family", NULL);
  665. if (family) {
  666. if (!strcmp(family, "virtex2p")) {
  667. regs = &v2_config_registers;
  668. } else if (!strcmp(family, "virtex4")) {
  669. regs = &v4_config_registers;
  670. } else if (!strcmp(family, "virtex5")) {
  671. regs = &v5_config_registers;
  672. }
  673. }
  674. return hwicap_setup(&op->dev, id ? *id : -1, &res, config,
  675. regs);
  676. }
  677. static int __devexit hwicap_of_remove(struct of_device *op)
  678. {
  679. return hwicap_remove(&op->dev);
  680. }
  681. /* Match table for of_platform binding */
  682. static const struct of_device_id __devinit hwicap_of_match[] = {
  683. { .compatible = "xlnx,opb-hwicap-1.00.b", .data = &buffer_icap_config},
  684. { .compatible = "xlnx,xps-hwicap-1.00.a", .data = &fifo_icap_config},
  685. {},
  686. };
  687. MODULE_DEVICE_TABLE(of, hwicap_of_match);
  688. static struct of_platform_driver hwicap_of_driver = {
  689. .owner = THIS_MODULE,
  690. .name = DRIVER_NAME,
  691. .match_table = hwicap_of_match,
  692. .probe = hwicap_of_probe,
  693. .remove = __devexit_p(hwicap_of_remove),
  694. .driver = {
  695. .name = DRIVER_NAME,
  696. },
  697. };
  698. /* Registration helpers to keep the number of #ifdefs to a minimum */
  699. static inline int __init hwicap_of_register(void)
  700. {
  701. pr_debug("hwicap: calling of_register_platform_driver()\n");
  702. return of_register_platform_driver(&hwicap_of_driver);
  703. }
  704. static inline void __exit hwicap_of_unregister(void)
  705. {
  706. of_unregister_platform_driver(&hwicap_of_driver);
  707. }
  708. #else /* CONFIG_OF */
  709. /* CONFIG_OF not enabled; do nothing helpers */
  710. static inline int __init hwicap_of_register(void) { return 0; }
  711. static inline void __exit hwicap_of_unregister(void) { }
  712. #endif /* CONFIG_OF */
  713. static int __init hwicap_module_init(void)
  714. {
  715. dev_t devt;
  716. int retval;
  717. icap_class = class_create(THIS_MODULE, "xilinx_config");
  718. mutex_init(&icap_sem);
  719. if (xhwicap_major) {
  720. devt = MKDEV(xhwicap_major, xhwicap_minor);
  721. retval = register_chrdev_region(
  722. devt,
  723. HWICAP_DEVICES,
  724. DRIVER_NAME);
  725. if (retval < 0)
  726. return retval;
  727. } else {
  728. retval = alloc_chrdev_region(&devt,
  729. xhwicap_minor,
  730. HWICAP_DEVICES,
  731. DRIVER_NAME);
  732. if (retval < 0)
  733. return retval;
  734. xhwicap_major = MAJOR(devt);
  735. }
  736. retval = platform_driver_register(&hwicap_platform_driver);
  737. if (retval)
  738. goto failed1;
  739. retval = hwicap_of_register();
  740. if (retval)
  741. goto failed2;
  742. return retval;
  743. failed2:
  744. platform_driver_unregister(&hwicap_platform_driver);
  745. failed1:
  746. unregister_chrdev_region(devt, HWICAP_DEVICES);
  747. return retval;
  748. }
  749. static void __exit hwicap_module_cleanup(void)
  750. {
  751. dev_t devt = MKDEV(xhwicap_major, xhwicap_minor);
  752. class_destroy(icap_class);
  753. platform_driver_unregister(&hwicap_platform_driver);
  754. hwicap_of_unregister();
  755. unregister_chrdev_region(devt, HWICAP_DEVICES);
  756. }
  757. module_init(hwicap_module_init);
  758. module_exit(hwicap_module_cleanup);
  759. MODULE_AUTHOR("Xilinx, Inc; Xilinx Research Labs Group");
  760. MODULE_DESCRIPTION("Xilinx ICAP Port Driver");
  761. MODULE_LICENSE("GPL");