xilinx_hwicap.c 22 KB

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