xilinx_hwicap.c 22 KB

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