xilinx_hwicap.c 22 KB

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