xilinx_hwicap.c 22 KB

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