xilinx_hwicap.c 22 KB

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