xilinx_hwicap.c 22 KB

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