xsysace.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /*
  2. * Xilinx SystemACE device driver
  3. *
  4. * Copyright 2007 Secret Lab Technologies Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. */
  10. /*
  11. * The SystemACE chip is designed to configure FPGAs by loading an FPGA
  12. * bitstream from a file on a CF card and squirting it into FPGAs connected
  13. * to the SystemACE JTAG chain. It also has the advantage of providing an
  14. * MPU interface which can be used to control the FPGA configuration process
  15. * and to use the attached CF card for general purpose storage.
  16. *
  17. * This driver is a block device driver for the SystemACE.
  18. *
  19. * Initialization:
  20. * The driver registers itself as a platform_device driver at module
  21. * load time. The platform bus will take care of calling the
  22. * ace_probe() method for all SystemACE instances in the system. Any
  23. * number of SystemACE instances are supported. ace_probe() calls
  24. * ace_setup() which initialized all data structures, reads the CF
  25. * id structure and registers the device.
  26. *
  27. * Processing:
  28. * Just about all of the heavy lifting in this driver is performed by
  29. * a Finite State Machine (FSM). The driver needs to wait on a number
  30. * of events; some raised by interrupts, some which need to be polled
  31. * for. Describing all of the behaviour in a FSM seems to be the
  32. * easiest way to keep the complexity low and make it easy to
  33. * understand what the driver is doing. If the block ops or the
  34. * request function need to interact with the hardware, then they
  35. * simply need to flag the request and kick of FSM processing.
  36. *
  37. * The FSM itself is atomic-safe code which can be run from any
  38. * context. The general process flow is:
  39. * 1. obtain the ace->lock spinlock.
  40. * 2. loop on ace_fsm_dostate() until the ace->fsm_continue flag is
  41. * cleared.
  42. * 3. release the lock.
  43. *
  44. * Individual states do not sleep in any way. If a condition needs to
  45. * be waited for then the state much clear the fsm_continue flag and
  46. * either schedule the FSM to be run again at a later time, or expect
  47. * an interrupt to call the FSM when the desired condition is met.
  48. *
  49. * In normal operation, the FSM is processed at interrupt context
  50. * either when the driver's tasklet is scheduled, or when an irq is
  51. * raised by the hardware. The tasklet can be scheduled at any time.
  52. * The request method in particular schedules the tasklet when a new
  53. * request has been indicated by the block layer. Once started, the
  54. * FSM proceeds as far as it can processing the request until it
  55. * needs on a hardware event. At this point, it must yield execution.
  56. *
  57. * A state has two options when yielding execution:
  58. * 1. ace_fsm_yield()
  59. * - Call if need to poll for event.
  60. * - clears the fsm_continue flag to exit the processing loop
  61. * - reschedules the tasklet to run again as soon as possible
  62. * 2. ace_fsm_yieldirq()
  63. * - Call if an irq is expected from the HW
  64. * - clears the fsm_continue flag to exit the processing loop
  65. * - does not reschedule the tasklet so the FSM will not be processed
  66. * again until an irq is received.
  67. * After calling a yield function, the state must return control back
  68. * to the FSM main loop.
  69. *
  70. * Additionally, the driver maintains a kernel timer which can process
  71. * the FSM. If the FSM gets stalled, typically due to a missed
  72. * interrupt, then the kernel timer will expire and the driver can
  73. * continue where it left off.
  74. *
  75. * To Do:
  76. * - Add FPGA configuration control interface.
  77. * - Request major number from lanana
  78. */
  79. #undef DEBUG
  80. #include <linux/module.h>
  81. #include <linux/ctype.h>
  82. #include <linux/init.h>
  83. #include <linux/interrupt.h>
  84. #include <linux/errno.h>
  85. #include <linux/kernel.h>
  86. #include <linux/delay.h>
  87. #include <linux/slab.h>
  88. #include <linux/blkdev.h>
  89. #include <linux/ata.h>
  90. #include <linux/hdreg.h>
  91. #include <linux/platform_device.h>
  92. #if defined(CONFIG_OF)
  93. #include <linux/of_device.h>
  94. #include <linux/of_platform.h>
  95. #endif
  96. MODULE_AUTHOR("Grant Likely <grant.likely@secretlab.ca>");
  97. MODULE_DESCRIPTION("Xilinx SystemACE device driver");
  98. MODULE_LICENSE("GPL");
  99. /* SystemACE register definitions */
  100. #define ACE_BUSMODE (0x00)
  101. #define ACE_STATUS (0x04)
  102. #define ACE_STATUS_CFGLOCK (0x00000001)
  103. #define ACE_STATUS_MPULOCK (0x00000002)
  104. #define ACE_STATUS_CFGERROR (0x00000004) /* config controller error */
  105. #define ACE_STATUS_CFCERROR (0x00000008) /* CF controller error */
  106. #define ACE_STATUS_CFDETECT (0x00000010)
  107. #define ACE_STATUS_DATABUFRDY (0x00000020)
  108. #define ACE_STATUS_DATABUFMODE (0x00000040)
  109. #define ACE_STATUS_CFGDONE (0x00000080)
  110. #define ACE_STATUS_RDYFORCFCMD (0x00000100)
  111. #define ACE_STATUS_CFGMODEPIN (0x00000200)
  112. #define ACE_STATUS_CFGADDR_MASK (0x0000e000)
  113. #define ACE_STATUS_CFBSY (0x00020000)
  114. #define ACE_STATUS_CFRDY (0x00040000)
  115. #define ACE_STATUS_CFDWF (0x00080000)
  116. #define ACE_STATUS_CFDSC (0x00100000)
  117. #define ACE_STATUS_CFDRQ (0x00200000)
  118. #define ACE_STATUS_CFCORR (0x00400000)
  119. #define ACE_STATUS_CFERR (0x00800000)
  120. #define ACE_ERROR (0x08)
  121. #define ACE_CFGLBA (0x0c)
  122. #define ACE_MPULBA (0x10)
  123. #define ACE_SECCNTCMD (0x14)
  124. #define ACE_SECCNTCMD_RESET (0x0100)
  125. #define ACE_SECCNTCMD_IDENTIFY (0x0200)
  126. #define ACE_SECCNTCMD_READ_DATA (0x0300)
  127. #define ACE_SECCNTCMD_WRITE_DATA (0x0400)
  128. #define ACE_SECCNTCMD_ABORT (0x0600)
  129. #define ACE_VERSION (0x16)
  130. #define ACE_VERSION_REVISION_MASK (0x00FF)
  131. #define ACE_VERSION_MINOR_MASK (0x0F00)
  132. #define ACE_VERSION_MAJOR_MASK (0xF000)
  133. #define ACE_CTRL (0x18)
  134. #define ACE_CTRL_FORCELOCKREQ (0x0001)
  135. #define ACE_CTRL_LOCKREQ (0x0002)
  136. #define ACE_CTRL_FORCECFGADDR (0x0004)
  137. #define ACE_CTRL_FORCECFGMODE (0x0008)
  138. #define ACE_CTRL_CFGMODE (0x0010)
  139. #define ACE_CTRL_CFGSTART (0x0020)
  140. #define ACE_CTRL_CFGSEL (0x0040)
  141. #define ACE_CTRL_CFGRESET (0x0080)
  142. #define ACE_CTRL_DATABUFRDYIRQ (0x0100)
  143. #define ACE_CTRL_ERRORIRQ (0x0200)
  144. #define ACE_CTRL_CFGDONEIRQ (0x0400)
  145. #define ACE_CTRL_RESETIRQ (0x0800)
  146. #define ACE_CTRL_CFGPROG (0x1000)
  147. #define ACE_CTRL_CFGADDR_MASK (0xe000)
  148. #define ACE_FATSTAT (0x1c)
  149. #define ACE_NUM_MINORS 16
  150. #define ACE_SECTOR_SIZE (512)
  151. #define ACE_FIFO_SIZE (32)
  152. #define ACE_BUF_PER_SECTOR (ACE_SECTOR_SIZE / ACE_FIFO_SIZE)
  153. #define ACE_BUS_WIDTH_8 0
  154. #define ACE_BUS_WIDTH_16 1
  155. struct ace_reg_ops;
  156. struct ace_device {
  157. /* driver state data */
  158. int id;
  159. int media_change;
  160. int users;
  161. struct list_head list;
  162. /* finite state machine data */
  163. struct tasklet_struct fsm_tasklet;
  164. uint fsm_task; /* Current activity (ACE_TASK_*) */
  165. uint fsm_state; /* Current state (ACE_FSM_STATE_*) */
  166. uint fsm_continue_flag; /* cleared to exit FSM mainloop */
  167. uint fsm_iter_num;
  168. struct timer_list stall_timer;
  169. /* Transfer state/result, use for both id and block request */
  170. struct request *req; /* request being processed */
  171. void *data_ptr; /* pointer to I/O buffer */
  172. int data_count; /* number of buffers remaining */
  173. int data_result; /* Result of transfer; 0 := success */
  174. int id_req_count; /* count of id requests */
  175. int id_result;
  176. struct completion id_completion; /* used when id req finishes */
  177. int in_irq;
  178. /* Details of hardware device */
  179. resource_size_t physaddr;
  180. void __iomem *baseaddr;
  181. int irq;
  182. int bus_width; /* 0 := 8 bit; 1 := 16 bit */
  183. struct ace_reg_ops *reg_ops;
  184. int lock_count;
  185. /* Block device data structures */
  186. spinlock_t lock;
  187. struct device *dev;
  188. struct request_queue *queue;
  189. struct gendisk *gd;
  190. /* Inserted CF card parameters */
  191. u16 cf_id[ATA_ID_WORDS];
  192. };
  193. static int ace_major;
  194. /* ---------------------------------------------------------------------
  195. * Low level register access
  196. */
  197. struct ace_reg_ops {
  198. u16(*in) (struct ace_device * ace, int reg);
  199. void (*out) (struct ace_device * ace, int reg, u16 val);
  200. void (*datain) (struct ace_device * ace);
  201. void (*dataout) (struct ace_device * ace);
  202. };
  203. /* 8 Bit bus width */
  204. static u16 ace_in_8(struct ace_device *ace, int reg)
  205. {
  206. void __iomem *r = ace->baseaddr + reg;
  207. return in_8(r) | (in_8(r + 1) << 8);
  208. }
  209. static void ace_out_8(struct ace_device *ace, int reg, u16 val)
  210. {
  211. void __iomem *r = ace->baseaddr + reg;
  212. out_8(r, val);
  213. out_8(r + 1, val >> 8);
  214. }
  215. static void ace_datain_8(struct ace_device *ace)
  216. {
  217. void __iomem *r = ace->baseaddr + 0x40;
  218. u8 *dst = ace->data_ptr;
  219. int i = ACE_FIFO_SIZE;
  220. while (i--)
  221. *dst++ = in_8(r++);
  222. ace->data_ptr = dst;
  223. }
  224. static void ace_dataout_8(struct ace_device *ace)
  225. {
  226. void __iomem *r = ace->baseaddr + 0x40;
  227. u8 *src = ace->data_ptr;
  228. int i = ACE_FIFO_SIZE;
  229. while (i--)
  230. out_8(r++, *src++);
  231. ace->data_ptr = src;
  232. }
  233. static struct ace_reg_ops ace_reg_8_ops = {
  234. .in = ace_in_8,
  235. .out = ace_out_8,
  236. .datain = ace_datain_8,
  237. .dataout = ace_dataout_8,
  238. };
  239. /* 16 bit big endian bus attachment */
  240. static u16 ace_in_be16(struct ace_device *ace, int reg)
  241. {
  242. return in_be16(ace->baseaddr + reg);
  243. }
  244. static void ace_out_be16(struct ace_device *ace, int reg, u16 val)
  245. {
  246. out_be16(ace->baseaddr + reg, val);
  247. }
  248. static void ace_datain_be16(struct ace_device *ace)
  249. {
  250. int i = ACE_FIFO_SIZE / 2;
  251. u16 *dst = ace->data_ptr;
  252. while (i--)
  253. *dst++ = in_le16(ace->baseaddr + 0x40);
  254. ace->data_ptr = dst;
  255. }
  256. static void ace_dataout_be16(struct ace_device *ace)
  257. {
  258. int i = ACE_FIFO_SIZE / 2;
  259. u16 *src = ace->data_ptr;
  260. while (i--)
  261. out_le16(ace->baseaddr + 0x40, *src++);
  262. ace->data_ptr = src;
  263. }
  264. /* 16 bit little endian bus attachment */
  265. static u16 ace_in_le16(struct ace_device *ace, int reg)
  266. {
  267. return in_le16(ace->baseaddr + reg);
  268. }
  269. static void ace_out_le16(struct ace_device *ace, int reg, u16 val)
  270. {
  271. out_le16(ace->baseaddr + reg, val);
  272. }
  273. static void ace_datain_le16(struct ace_device *ace)
  274. {
  275. int i = ACE_FIFO_SIZE / 2;
  276. u16 *dst = ace->data_ptr;
  277. while (i--)
  278. *dst++ = in_be16(ace->baseaddr + 0x40);
  279. ace->data_ptr = dst;
  280. }
  281. static void ace_dataout_le16(struct ace_device *ace)
  282. {
  283. int i = ACE_FIFO_SIZE / 2;
  284. u16 *src = ace->data_ptr;
  285. while (i--)
  286. out_be16(ace->baseaddr + 0x40, *src++);
  287. ace->data_ptr = src;
  288. }
  289. static struct ace_reg_ops ace_reg_be16_ops = {
  290. .in = ace_in_be16,
  291. .out = ace_out_be16,
  292. .datain = ace_datain_be16,
  293. .dataout = ace_dataout_be16,
  294. };
  295. static struct ace_reg_ops ace_reg_le16_ops = {
  296. .in = ace_in_le16,
  297. .out = ace_out_le16,
  298. .datain = ace_datain_le16,
  299. .dataout = ace_dataout_le16,
  300. };
  301. static inline u16 ace_in(struct ace_device *ace, int reg)
  302. {
  303. return ace->reg_ops->in(ace, reg);
  304. }
  305. static inline u32 ace_in32(struct ace_device *ace, int reg)
  306. {
  307. return ace_in(ace, reg) | (ace_in(ace, reg + 2) << 16);
  308. }
  309. static inline void ace_out(struct ace_device *ace, int reg, u16 val)
  310. {
  311. ace->reg_ops->out(ace, reg, val);
  312. }
  313. static inline void ace_out32(struct ace_device *ace, int reg, u32 val)
  314. {
  315. ace_out(ace, reg, val);
  316. ace_out(ace, reg + 2, val >> 16);
  317. }
  318. /* ---------------------------------------------------------------------
  319. * Debug support functions
  320. */
  321. #if defined(DEBUG)
  322. static void ace_dump_mem(void *base, int len)
  323. {
  324. const char *ptr = base;
  325. int i, j;
  326. for (i = 0; i < len; i += 16) {
  327. printk(KERN_INFO "%.8x:", i);
  328. for (j = 0; j < 16; j++) {
  329. if (!(j % 4))
  330. printk(" ");
  331. printk("%.2x", ptr[i + j]);
  332. }
  333. printk(" ");
  334. for (j = 0; j < 16; j++)
  335. printk("%c", isprint(ptr[i + j]) ? ptr[i + j] : '.');
  336. printk("\n");
  337. }
  338. }
  339. #else
  340. static inline void ace_dump_mem(void *base, int len)
  341. {
  342. }
  343. #endif
  344. static void ace_dump_regs(struct ace_device *ace)
  345. {
  346. dev_info(ace->dev, " ctrl: %.8x seccnt/cmd: %.4x ver:%.4x\n"
  347. KERN_INFO " status:%.8x mpu_lba:%.8x busmode:%4x\n"
  348. KERN_INFO " error: %.8x cfg_lba:%.8x fatstat:%.4x\n",
  349. ace_in32(ace, ACE_CTRL),
  350. ace_in(ace, ACE_SECCNTCMD),
  351. ace_in(ace, ACE_VERSION),
  352. ace_in32(ace, ACE_STATUS),
  353. ace_in32(ace, ACE_MPULBA),
  354. ace_in(ace, ACE_BUSMODE),
  355. ace_in32(ace, ACE_ERROR),
  356. ace_in32(ace, ACE_CFGLBA), ace_in(ace, ACE_FATSTAT));
  357. }
  358. void ace_fix_driveid(u16 *id)
  359. {
  360. #if defined(__BIG_ENDIAN)
  361. int i;
  362. /* All half words have wrong byte order; swap the bytes */
  363. for (i = 0; i < ATA_ID_WORDS; i++, id++)
  364. *id = le16_to_cpu(*id);
  365. #endif
  366. }
  367. /* ---------------------------------------------------------------------
  368. * Finite State Machine (FSM) implementation
  369. */
  370. /* FSM tasks; used to direct state transitions */
  371. #define ACE_TASK_IDLE 0
  372. #define ACE_TASK_IDENTIFY 1
  373. #define ACE_TASK_READ 2
  374. #define ACE_TASK_WRITE 3
  375. #define ACE_FSM_NUM_TASKS 4
  376. /* FSM state definitions */
  377. #define ACE_FSM_STATE_IDLE 0
  378. #define ACE_FSM_STATE_REQ_LOCK 1
  379. #define ACE_FSM_STATE_WAIT_LOCK 2
  380. #define ACE_FSM_STATE_WAIT_CFREADY 3
  381. #define ACE_FSM_STATE_IDENTIFY_PREPARE 4
  382. #define ACE_FSM_STATE_IDENTIFY_TRANSFER 5
  383. #define ACE_FSM_STATE_IDENTIFY_COMPLETE 6
  384. #define ACE_FSM_STATE_REQ_PREPARE 7
  385. #define ACE_FSM_STATE_REQ_TRANSFER 8
  386. #define ACE_FSM_STATE_REQ_COMPLETE 9
  387. #define ACE_FSM_STATE_ERROR 10
  388. #define ACE_FSM_NUM_STATES 11
  389. /* Set flag to exit FSM loop and reschedule tasklet */
  390. static inline void ace_fsm_yield(struct ace_device *ace)
  391. {
  392. dev_dbg(ace->dev, "ace_fsm_yield()\n");
  393. tasklet_schedule(&ace->fsm_tasklet);
  394. ace->fsm_continue_flag = 0;
  395. }
  396. /* Set flag to exit FSM loop and wait for IRQ to reschedule tasklet */
  397. static inline void ace_fsm_yieldirq(struct ace_device *ace)
  398. {
  399. dev_dbg(ace->dev, "ace_fsm_yieldirq()\n");
  400. if (ace->irq == NO_IRQ)
  401. /* No IRQ assigned, so need to poll */
  402. tasklet_schedule(&ace->fsm_tasklet);
  403. ace->fsm_continue_flag = 0;
  404. }
  405. /* Get the next read/write request; ending requests that we don't handle */
  406. struct request *ace_get_next_request(struct request_queue * q)
  407. {
  408. struct request *req;
  409. while ((req = elv_next_request(q)) != NULL) {
  410. if (blk_fs_request(req))
  411. break;
  412. blkdev_dequeue_request(req);
  413. __blk_end_request_all(req, -EIO);
  414. }
  415. return req;
  416. }
  417. static void ace_fsm_dostate(struct ace_device *ace)
  418. {
  419. struct request *req;
  420. u32 status;
  421. u16 val;
  422. int count;
  423. #if defined(DEBUG)
  424. dev_dbg(ace->dev, "fsm_state=%i, id_req_count=%i\n",
  425. ace->fsm_state, ace->id_req_count);
  426. #endif
  427. /* Verify that there is actually a CF in the slot. If not, then
  428. * bail out back to the idle state and wake up all the waiters */
  429. status = ace_in32(ace, ACE_STATUS);
  430. if ((status & ACE_STATUS_CFDETECT) == 0) {
  431. ace->fsm_state = ACE_FSM_STATE_IDLE;
  432. ace->media_change = 1;
  433. set_capacity(ace->gd, 0);
  434. dev_info(ace->dev, "No CF in slot\n");
  435. /* Drop all in-flight and pending requests */
  436. if (ace->req) {
  437. __blk_end_request_all(ace->req, -EIO);
  438. ace->req = NULL;
  439. }
  440. while ((req = elv_next_request(ace->queue)) != NULL) {
  441. blkdev_dequeue_request(req);
  442. __blk_end_request_all(req, -EIO);
  443. }
  444. /* Drop back to IDLE state and notify waiters */
  445. ace->fsm_state = ACE_FSM_STATE_IDLE;
  446. ace->id_result = -EIO;
  447. while (ace->id_req_count) {
  448. complete(&ace->id_completion);
  449. ace->id_req_count--;
  450. }
  451. }
  452. switch (ace->fsm_state) {
  453. case ACE_FSM_STATE_IDLE:
  454. /* See if there is anything to do */
  455. if (ace->id_req_count || ace_get_next_request(ace->queue)) {
  456. ace->fsm_iter_num++;
  457. ace->fsm_state = ACE_FSM_STATE_REQ_LOCK;
  458. mod_timer(&ace->stall_timer, jiffies + HZ);
  459. if (!timer_pending(&ace->stall_timer))
  460. add_timer(&ace->stall_timer);
  461. break;
  462. }
  463. del_timer(&ace->stall_timer);
  464. ace->fsm_continue_flag = 0;
  465. break;
  466. case ACE_FSM_STATE_REQ_LOCK:
  467. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  468. /* Already have the lock, jump to next state */
  469. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  470. break;
  471. }
  472. /* Request the lock */
  473. val = ace_in(ace, ACE_CTRL);
  474. ace_out(ace, ACE_CTRL, val | ACE_CTRL_LOCKREQ);
  475. ace->fsm_state = ACE_FSM_STATE_WAIT_LOCK;
  476. break;
  477. case ACE_FSM_STATE_WAIT_LOCK:
  478. if (ace_in(ace, ACE_STATUS) & ACE_STATUS_MPULOCK) {
  479. /* got the lock; move to next state */
  480. ace->fsm_state = ACE_FSM_STATE_WAIT_CFREADY;
  481. break;
  482. }
  483. /* wait a bit for the lock */
  484. ace_fsm_yield(ace);
  485. break;
  486. case ACE_FSM_STATE_WAIT_CFREADY:
  487. status = ace_in32(ace, ACE_STATUS);
  488. if (!(status & ACE_STATUS_RDYFORCFCMD) ||
  489. (status & ACE_STATUS_CFBSY)) {
  490. /* CF card isn't ready; it needs to be polled */
  491. ace_fsm_yield(ace);
  492. break;
  493. }
  494. /* Device is ready for command; determine what to do next */
  495. if (ace->id_req_count)
  496. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_PREPARE;
  497. else
  498. ace->fsm_state = ACE_FSM_STATE_REQ_PREPARE;
  499. break;
  500. case ACE_FSM_STATE_IDENTIFY_PREPARE:
  501. /* Send identify command */
  502. ace->fsm_task = ACE_TASK_IDENTIFY;
  503. ace->data_ptr = ace->cf_id;
  504. ace->data_count = ACE_BUF_PER_SECTOR;
  505. ace_out(ace, ACE_SECCNTCMD, ACE_SECCNTCMD_IDENTIFY);
  506. /* As per datasheet, put config controller in reset */
  507. val = ace_in(ace, ACE_CTRL);
  508. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  509. /* irq handler takes over from this point; wait for the
  510. * transfer to complete */
  511. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_TRANSFER;
  512. ace_fsm_yieldirq(ace);
  513. break;
  514. case ACE_FSM_STATE_IDENTIFY_TRANSFER:
  515. /* Check that the sysace is ready to receive data */
  516. status = ace_in32(ace, ACE_STATUS);
  517. if (status & ACE_STATUS_CFBSY) {
  518. dev_dbg(ace->dev, "CFBSY set; t=%i iter=%i dc=%i\n",
  519. ace->fsm_task, ace->fsm_iter_num,
  520. ace->data_count);
  521. ace_fsm_yield(ace);
  522. break;
  523. }
  524. if (!(status & ACE_STATUS_DATABUFRDY)) {
  525. ace_fsm_yield(ace);
  526. break;
  527. }
  528. /* Transfer the next buffer */
  529. ace->reg_ops->datain(ace);
  530. ace->data_count--;
  531. /* If there are still buffers to be transfers; jump out here */
  532. if (ace->data_count != 0) {
  533. ace_fsm_yieldirq(ace);
  534. break;
  535. }
  536. /* transfer finished; kick state machine */
  537. dev_dbg(ace->dev, "identify finished\n");
  538. ace->fsm_state = ACE_FSM_STATE_IDENTIFY_COMPLETE;
  539. break;
  540. case ACE_FSM_STATE_IDENTIFY_COMPLETE:
  541. ace_fix_driveid(ace->cf_id);
  542. ace_dump_mem(ace->cf_id, 512); /* Debug: Dump out disk ID */
  543. if (ace->data_result) {
  544. /* Error occured, disable the disk */
  545. ace->media_change = 1;
  546. set_capacity(ace->gd, 0);
  547. dev_err(ace->dev, "error fetching CF id (%i)\n",
  548. ace->data_result);
  549. } else {
  550. ace->media_change = 0;
  551. /* Record disk parameters */
  552. set_capacity(ace->gd,
  553. ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
  554. dev_info(ace->dev, "capacity: %i sectors\n",
  555. ata_id_u32(ace->cf_id, ATA_ID_LBA_CAPACITY));
  556. }
  557. /* We're done, drop to IDLE state and notify waiters */
  558. ace->fsm_state = ACE_FSM_STATE_IDLE;
  559. ace->id_result = ace->data_result;
  560. while (ace->id_req_count) {
  561. complete(&ace->id_completion);
  562. ace->id_req_count--;
  563. }
  564. break;
  565. case ACE_FSM_STATE_REQ_PREPARE:
  566. req = ace_get_next_request(ace->queue);
  567. if (!req) {
  568. ace->fsm_state = ACE_FSM_STATE_IDLE;
  569. break;
  570. }
  571. blkdev_dequeue_request(req);
  572. /* Okay, it's a data request, set it up for transfer */
  573. dev_dbg(ace->dev,
  574. "request: sec=%llx hcnt=%x, ccnt=%x, dir=%i\n",
  575. (unsigned long long)blk_rq_pos(req),
  576. blk_rq_sectors(req), blk_rq_cur_sectors(req),
  577. rq_data_dir(req));
  578. ace->req = req;
  579. ace->data_ptr = req->buffer;
  580. ace->data_count = blk_rq_cur_sectors(req) * ACE_BUF_PER_SECTOR;
  581. ace_out32(ace, ACE_MPULBA, blk_rq_pos(req) & 0x0FFFFFFF);
  582. count = blk_rq_sectors(req);
  583. if (rq_data_dir(req)) {
  584. /* Kick off write request */
  585. dev_dbg(ace->dev, "write data\n");
  586. ace->fsm_task = ACE_TASK_WRITE;
  587. ace_out(ace, ACE_SECCNTCMD,
  588. count | ACE_SECCNTCMD_WRITE_DATA);
  589. } else {
  590. /* Kick off read request */
  591. dev_dbg(ace->dev, "read data\n");
  592. ace->fsm_task = ACE_TASK_READ;
  593. ace_out(ace, ACE_SECCNTCMD,
  594. count | ACE_SECCNTCMD_READ_DATA);
  595. }
  596. /* As per datasheet, put config controller in reset */
  597. val = ace_in(ace, ACE_CTRL);
  598. ace_out(ace, ACE_CTRL, val | ACE_CTRL_CFGRESET);
  599. /* Move to the transfer state. The systemace will raise
  600. * an interrupt once there is something to do
  601. */
  602. ace->fsm_state = ACE_FSM_STATE_REQ_TRANSFER;
  603. if (ace->fsm_task == ACE_TASK_READ)
  604. ace_fsm_yieldirq(ace); /* wait for data ready */
  605. break;
  606. case ACE_FSM_STATE_REQ_TRANSFER:
  607. /* Check that the sysace is ready to receive data */
  608. status = ace_in32(ace, ACE_STATUS);
  609. if (status & ACE_STATUS_CFBSY) {
  610. dev_dbg(ace->dev,
  611. "CFBSY set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  612. ace->fsm_task, ace->fsm_iter_num,
  613. blk_rq_cur_sectors(ace->req) * 16,
  614. ace->data_count, ace->in_irq);
  615. ace_fsm_yield(ace); /* need to poll CFBSY bit */
  616. break;
  617. }
  618. if (!(status & ACE_STATUS_DATABUFRDY)) {
  619. dev_dbg(ace->dev,
  620. "DATABUF not set; t=%i iter=%i c=%i dc=%i irq=%i\n",
  621. ace->fsm_task, ace->fsm_iter_num,
  622. blk_rq_cur_sectors(ace->req) * 16,
  623. ace->data_count, ace->in_irq);
  624. ace_fsm_yieldirq(ace);
  625. break;
  626. }
  627. /* Transfer the next buffer */
  628. if (ace->fsm_task == ACE_TASK_WRITE)
  629. ace->reg_ops->dataout(ace);
  630. else
  631. ace->reg_ops->datain(ace);
  632. ace->data_count--;
  633. /* If there are still buffers to be transfers; jump out here */
  634. if (ace->data_count != 0) {
  635. ace_fsm_yieldirq(ace);
  636. break;
  637. }
  638. /* bio finished; is there another one? */
  639. if (__blk_end_request_cur(ace->req, 0)) {
  640. /* dev_dbg(ace->dev, "next block; h=%u c=%u\n",
  641. * blk_rq_sectors(ace->req),
  642. * blk_rq_cur_sectors(ace->req));
  643. */
  644. ace->data_ptr = ace->req->buffer;
  645. ace->data_count = blk_rq_cur_sectors(ace->req) * 16;
  646. ace_fsm_yieldirq(ace);
  647. break;
  648. }
  649. ace->fsm_state = ACE_FSM_STATE_REQ_COMPLETE;
  650. break;
  651. case ACE_FSM_STATE_REQ_COMPLETE:
  652. ace->req = NULL;
  653. /* Finished request; go to idle state */
  654. ace->fsm_state = ACE_FSM_STATE_IDLE;
  655. break;
  656. default:
  657. ace->fsm_state = ACE_FSM_STATE_IDLE;
  658. break;
  659. }
  660. }
  661. static void ace_fsm_tasklet(unsigned long data)
  662. {
  663. struct ace_device *ace = (void *)data;
  664. unsigned long flags;
  665. spin_lock_irqsave(&ace->lock, flags);
  666. /* Loop over state machine until told to stop */
  667. ace->fsm_continue_flag = 1;
  668. while (ace->fsm_continue_flag)
  669. ace_fsm_dostate(ace);
  670. spin_unlock_irqrestore(&ace->lock, flags);
  671. }
  672. static void ace_stall_timer(unsigned long data)
  673. {
  674. struct ace_device *ace = (void *)data;
  675. unsigned long flags;
  676. dev_warn(ace->dev,
  677. "kicking stalled fsm; state=%i task=%i iter=%i dc=%i\n",
  678. ace->fsm_state, ace->fsm_task, ace->fsm_iter_num,
  679. ace->data_count);
  680. spin_lock_irqsave(&ace->lock, flags);
  681. /* Rearm the stall timer *before* entering FSM (which may then
  682. * delete the timer) */
  683. mod_timer(&ace->stall_timer, jiffies + HZ);
  684. /* Loop over state machine until told to stop */
  685. ace->fsm_continue_flag = 1;
  686. while (ace->fsm_continue_flag)
  687. ace_fsm_dostate(ace);
  688. spin_unlock_irqrestore(&ace->lock, flags);
  689. }
  690. /* ---------------------------------------------------------------------
  691. * Interrupt handling routines
  692. */
  693. static int ace_interrupt_checkstate(struct ace_device *ace)
  694. {
  695. u32 sreg = ace_in32(ace, ACE_STATUS);
  696. u16 creg = ace_in(ace, ACE_CTRL);
  697. /* Check for error occurance */
  698. if ((sreg & (ACE_STATUS_CFGERROR | ACE_STATUS_CFCERROR)) &&
  699. (creg & ACE_CTRL_ERRORIRQ)) {
  700. dev_err(ace->dev, "transfer failure\n");
  701. ace_dump_regs(ace);
  702. return -EIO;
  703. }
  704. return 0;
  705. }
  706. static irqreturn_t ace_interrupt(int irq, void *dev_id)
  707. {
  708. u16 creg;
  709. struct ace_device *ace = dev_id;
  710. /* be safe and get the lock */
  711. spin_lock(&ace->lock);
  712. ace->in_irq = 1;
  713. /* clear the interrupt */
  714. creg = ace_in(ace, ACE_CTRL);
  715. ace_out(ace, ACE_CTRL, creg | ACE_CTRL_RESETIRQ);
  716. ace_out(ace, ACE_CTRL, creg);
  717. /* check for IO failures */
  718. if (ace_interrupt_checkstate(ace))
  719. ace->data_result = -EIO;
  720. if (ace->fsm_task == 0) {
  721. dev_err(ace->dev,
  722. "spurious irq; stat=%.8x ctrl=%.8x cmd=%.4x\n",
  723. ace_in32(ace, ACE_STATUS), ace_in32(ace, ACE_CTRL),
  724. ace_in(ace, ACE_SECCNTCMD));
  725. dev_err(ace->dev, "fsm_task=%i fsm_state=%i data_count=%i\n",
  726. ace->fsm_task, ace->fsm_state, ace->data_count);
  727. }
  728. /* Loop over state machine until told to stop */
  729. ace->fsm_continue_flag = 1;
  730. while (ace->fsm_continue_flag)
  731. ace_fsm_dostate(ace);
  732. /* done with interrupt; drop the lock */
  733. ace->in_irq = 0;
  734. spin_unlock(&ace->lock);
  735. return IRQ_HANDLED;
  736. }
  737. /* ---------------------------------------------------------------------
  738. * Block ops
  739. */
  740. static void ace_request(struct request_queue * q)
  741. {
  742. struct request *req;
  743. struct ace_device *ace;
  744. req = ace_get_next_request(q);
  745. if (req) {
  746. ace = req->rq_disk->private_data;
  747. tasklet_schedule(&ace->fsm_tasklet);
  748. }
  749. }
  750. static int ace_media_changed(struct gendisk *gd)
  751. {
  752. struct ace_device *ace = gd->private_data;
  753. dev_dbg(ace->dev, "ace_media_changed(): %i\n", ace->media_change);
  754. return ace->media_change;
  755. }
  756. static int ace_revalidate_disk(struct gendisk *gd)
  757. {
  758. struct ace_device *ace = gd->private_data;
  759. unsigned long flags;
  760. dev_dbg(ace->dev, "ace_revalidate_disk()\n");
  761. if (ace->media_change) {
  762. dev_dbg(ace->dev, "requesting cf id and scheduling tasklet\n");
  763. spin_lock_irqsave(&ace->lock, flags);
  764. ace->id_req_count++;
  765. spin_unlock_irqrestore(&ace->lock, flags);
  766. tasklet_schedule(&ace->fsm_tasklet);
  767. wait_for_completion(&ace->id_completion);
  768. }
  769. dev_dbg(ace->dev, "revalidate complete\n");
  770. return ace->id_result;
  771. }
  772. static int ace_open(struct block_device *bdev, fmode_t mode)
  773. {
  774. struct ace_device *ace = bdev->bd_disk->private_data;
  775. unsigned long flags;
  776. dev_dbg(ace->dev, "ace_open() users=%i\n", ace->users + 1);
  777. spin_lock_irqsave(&ace->lock, flags);
  778. ace->users++;
  779. spin_unlock_irqrestore(&ace->lock, flags);
  780. check_disk_change(bdev);
  781. return 0;
  782. }
  783. static int ace_release(struct gendisk *disk, fmode_t mode)
  784. {
  785. struct ace_device *ace = disk->private_data;
  786. unsigned long flags;
  787. u16 val;
  788. dev_dbg(ace->dev, "ace_release() users=%i\n", ace->users - 1);
  789. spin_lock_irqsave(&ace->lock, flags);
  790. ace->users--;
  791. if (ace->users == 0) {
  792. val = ace_in(ace, ACE_CTRL);
  793. ace_out(ace, ACE_CTRL, val & ~ACE_CTRL_LOCKREQ);
  794. }
  795. spin_unlock_irqrestore(&ace->lock, flags);
  796. return 0;
  797. }
  798. static int ace_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  799. {
  800. struct ace_device *ace = bdev->bd_disk->private_data;
  801. u16 *cf_id = ace->cf_id;
  802. dev_dbg(ace->dev, "ace_getgeo()\n");
  803. geo->heads = cf_id[ATA_ID_HEADS];
  804. geo->sectors = cf_id[ATA_ID_SECTORS];
  805. geo->cylinders = cf_id[ATA_ID_CYLS];
  806. return 0;
  807. }
  808. static struct block_device_operations ace_fops = {
  809. .owner = THIS_MODULE,
  810. .open = ace_open,
  811. .release = ace_release,
  812. .media_changed = ace_media_changed,
  813. .revalidate_disk = ace_revalidate_disk,
  814. .getgeo = ace_getgeo,
  815. };
  816. /* --------------------------------------------------------------------
  817. * SystemACE device setup/teardown code
  818. */
  819. static int __devinit ace_setup(struct ace_device *ace)
  820. {
  821. u16 version;
  822. u16 val;
  823. int rc;
  824. dev_dbg(ace->dev, "ace_setup(ace=0x%p)\n", ace);
  825. dev_dbg(ace->dev, "physaddr=0x%llx irq=%i\n",
  826. (unsigned long long)ace->physaddr, ace->irq);
  827. spin_lock_init(&ace->lock);
  828. init_completion(&ace->id_completion);
  829. /*
  830. * Map the device
  831. */
  832. ace->baseaddr = ioremap(ace->physaddr, 0x80);
  833. if (!ace->baseaddr)
  834. goto err_ioremap;
  835. /*
  836. * Initialize the state machine tasklet and stall timer
  837. */
  838. tasklet_init(&ace->fsm_tasklet, ace_fsm_tasklet, (unsigned long)ace);
  839. setup_timer(&ace->stall_timer, ace_stall_timer, (unsigned long)ace);
  840. /*
  841. * Initialize the request queue
  842. */
  843. ace->queue = blk_init_queue(ace_request, &ace->lock);
  844. if (ace->queue == NULL)
  845. goto err_blk_initq;
  846. blk_queue_hardsect_size(ace->queue, 512);
  847. /*
  848. * Allocate and initialize GD structure
  849. */
  850. ace->gd = alloc_disk(ACE_NUM_MINORS);
  851. if (!ace->gd)
  852. goto err_alloc_disk;
  853. ace->gd->major = ace_major;
  854. ace->gd->first_minor = ace->id * ACE_NUM_MINORS;
  855. ace->gd->fops = &ace_fops;
  856. ace->gd->queue = ace->queue;
  857. ace->gd->private_data = ace;
  858. snprintf(ace->gd->disk_name, 32, "xs%c", ace->id + 'a');
  859. /* set bus width */
  860. if (ace->bus_width == ACE_BUS_WIDTH_16) {
  861. /* 0x0101 should work regardless of endianess */
  862. ace_out_le16(ace, ACE_BUSMODE, 0x0101);
  863. /* read it back to determine endianess */
  864. if (ace_in_le16(ace, ACE_BUSMODE) == 0x0001)
  865. ace->reg_ops = &ace_reg_le16_ops;
  866. else
  867. ace->reg_ops = &ace_reg_be16_ops;
  868. } else {
  869. ace_out_8(ace, ACE_BUSMODE, 0x00);
  870. ace->reg_ops = &ace_reg_8_ops;
  871. }
  872. /* Make sure version register is sane */
  873. version = ace_in(ace, ACE_VERSION);
  874. if ((version == 0) || (version == 0xFFFF))
  875. goto err_read;
  876. /* Put sysace in a sane state by clearing most control reg bits */
  877. ace_out(ace, ACE_CTRL, ACE_CTRL_FORCECFGMODE |
  878. ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ);
  879. /* Now we can hook up the irq handler */
  880. if (ace->irq != NO_IRQ) {
  881. rc = request_irq(ace->irq, ace_interrupt, 0, "systemace", ace);
  882. if (rc) {
  883. /* Failure - fall back to polled mode */
  884. dev_err(ace->dev, "request_irq failed\n");
  885. ace->irq = NO_IRQ;
  886. }
  887. }
  888. /* Enable interrupts */
  889. val = ace_in(ace, ACE_CTRL);
  890. val |= ACE_CTRL_DATABUFRDYIRQ | ACE_CTRL_ERRORIRQ;
  891. ace_out(ace, ACE_CTRL, val);
  892. /* Print the identification */
  893. dev_info(ace->dev, "Xilinx SystemACE revision %i.%i.%i\n",
  894. (version >> 12) & 0xf, (version >> 8) & 0x0f, version & 0xff);
  895. dev_dbg(ace->dev, "physaddr 0x%llx, mapped to 0x%p, irq=%i\n",
  896. (unsigned long long) ace->physaddr, ace->baseaddr, ace->irq);
  897. ace->media_change = 1;
  898. ace_revalidate_disk(ace->gd);
  899. /* Make the sysace device 'live' */
  900. add_disk(ace->gd);
  901. return 0;
  902. err_read:
  903. put_disk(ace->gd);
  904. err_alloc_disk:
  905. blk_cleanup_queue(ace->queue);
  906. err_blk_initq:
  907. iounmap(ace->baseaddr);
  908. err_ioremap:
  909. dev_info(ace->dev, "xsysace: error initializing device at 0x%llx\n",
  910. (unsigned long long) ace->physaddr);
  911. return -ENOMEM;
  912. }
  913. static void __devexit ace_teardown(struct ace_device *ace)
  914. {
  915. if (ace->gd) {
  916. del_gendisk(ace->gd);
  917. put_disk(ace->gd);
  918. }
  919. if (ace->queue)
  920. blk_cleanup_queue(ace->queue);
  921. tasklet_kill(&ace->fsm_tasklet);
  922. if (ace->irq != NO_IRQ)
  923. free_irq(ace->irq, ace);
  924. iounmap(ace->baseaddr);
  925. }
  926. static int __devinit
  927. ace_alloc(struct device *dev, int id, resource_size_t physaddr,
  928. int irq, int bus_width)
  929. {
  930. struct ace_device *ace;
  931. int rc;
  932. dev_dbg(dev, "ace_alloc(%p)\n", dev);
  933. if (!physaddr) {
  934. rc = -ENODEV;
  935. goto err_noreg;
  936. }
  937. /* Allocate and initialize the ace device structure */
  938. ace = kzalloc(sizeof(struct ace_device), GFP_KERNEL);
  939. if (!ace) {
  940. rc = -ENOMEM;
  941. goto err_alloc;
  942. }
  943. ace->dev = dev;
  944. ace->id = id;
  945. ace->physaddr = physaddr;
  946. ace->irq = irq;
  947. ace->bus_width = bus_width;
  948. /* Call the setup code */
  949. rc = ace_setup(ace);
  950. if (rc)
  951. goto err_setup;
  952. dev_set_drvdata(dev, ace);
  953. return 0;
  954. err_setup:
  955. dev_set_drvdata(dev, NULL);
  956. kfree(ace);
  957. err_alloc:
  958. err_noreg:
  959. dev_err(dev, "could not initialize device, err=%i\n", rc);
  960. return rc;
  961. }
  962. static void __devexit ace_free(struct device *dev)
  963. {
  964. struct ace_device *ace = dev_get_drvdata(dev);
  965. dev_dbg(dev, "ace_free(%p)\n", dev);
  966. if (ace) {
  967. ace_teardown(ace);
  968. dev_set_drvdata(dev, NULL);
  969. kfree(ace);
  970. }
  971. }
  972. /* ---------------------------------------------------------------------
  973. * Platform Bus Support
  974. */
  975. static int __devinit ace_probe(struct platform_device *dev)
  976. {
  977. resource_size_t physaddr = 0;
  978. int bus_width = ACE_BUS_WIDTH_16; /* FIXME: should not be hard coded */
  979. int id = dev->id;
  980. int irq = NO_IRQ;
  981. int i;
  982. dev_dbg(&dev->dev, "ace_probe(%p)\n", dev);
  983. for (i = 0; i < dev->num_resources; i++) {
  984. if (dev->resource[i].flags & IORESOURCE_MEM)
  985. physaddr = dev->resource[i].start;
  986. if (dev->resource[i].flags & IORESOURCE_IRQ)
  987. irq = dev->resource[i].start;
  988. }
  989. /* Call the bus-independant setup code */
  990. return ace_alloc(&dev->dev, id, physaddr, irq, bus_width);
  991. }
  992. /*
  993. * Platform bus remove() method
  994. */
  995. static int __devexit ace_remove(struct platform_device *dev)
  996. {
  997. ace_free(&dev->dev);
  998. return 0;
  999. }
  1000. static struct platform_driver ace_platform_driver = {
  1001. .probe = ace_probe,
  1002. .remove = __devexit_p(ace_remove),
  1003. .driver = {
  1004. .owner = THIS_MODULE,
  1005. .name = "xsysace",
  1006. },
  1007. };
  1008. /* ---------------------------------------------------------------------
  1009. * OF_Platform Bus Support
  1010. */
  1011. #if defined(CONFIG_OF)
  1012. static int __devinit
  1013. ace_of_probe(struct of_device *op, const struct of_device_id *match)
  1014. {
  1015. struct resource res;
  1016. resource_size_t physaddr;
  1017. const u32 *id;
  1018. int irq, bus_width, rc;
  1019. dev_dbg(&op->dev, "ace_of_probe(%p, %p)\n", op, match);
  1020. /* device id */
  1021. id = of_get_property(op->node, "port-number", NULL);
  1022. /* physaddr */
  1023. rc = of_address_to_resource(op->node, 0, &res);
  1024. if (rc) {
  1025. dev_err(&op->dev, "invalid address\n");
  1026. return rc;
  1027. }
  1028. physaddr = res.start;
  1029. /* irq */
  1030. irq = irq_of_parse_and_map(op->node, 0);
  1031. /* bus width */
  1032. bus_width = ACE_BUS_WIDTH_16;
  1033. if (of_find_property(op->node, "8-bit", NULL))
  1034. bus_width = ACE_BUS_WIDTH_8;
  1035. /* Call the bus-independant setup code */
  1036. return ace_alloc(&op->dev, id ? *id : 0, physaddr, irq, bus_width);
  1037. }
  1038. static int __devexit ace_of_remove(struct of_device *op)
  1039. {
  1040. ace_free(&op->dev);
  1041. return 0;
  1042. }
  1043. /* Match table for of_platform binding */
  1044. static struct of_device_id ace_of_match[] __devinitdata = {
  1045. { .compatible = "xlnx,opb-sysace-1.00.b", },
  1046. { .compatible = "xlnx,opb-sysace-1.00.c", },
  1047. { .compatible = "xlnx,xps-sysace-1.00.a", },
  1048. { .compatible = "xlnx,sysace", },
  1049. {},
  1050. };
  1051. MODULE_DEVICE_TABLE(of, ace_of_match);
  1052. static struct of_platform_driver ace_of_driver = {
  1053. .owner = THIS_MODULE,
  1054. .name = "xsysace",
  1055. .match_table = ace_of_match,
  1056. .probe = ace_of_probe,
  1057. .remove = __devexit_p(ace_of_remove),
  1058. .driver = {
  1059. .name = "xsysace",
  1060. },
  1061. };
  1062. /* Registration helpers to keep the number of #ifdefs to a minimum */
  1063. static inline int __init ace_of_register(void)
  1064. {
  1065. pr_debug("xsysace: registering OF binding\n");
  1066. return of_register_platform_driver(&ace_of_driver);
  1067. }
  1068. static inline void __exit ace_of_unregister(void)
  1069. {
  1070. of_unregister_platform_driver(&ace_of_driver);
  1071. }
  1072. #else /* CONFIG_OF */
  1073. /* CONFIG_OF not enabled; do nothing helpers */
  1074. static inline int __init ace_of_register(void) { return 0; }
  1075. static inline void __exit ace_of_unregister(void) { }
  1076. #endif /* CONFIG_OF */
  1077. /* ---------------------------------------------------------------------
  1078. * Module init/exit routines
  1079. */
  1080. static int __init ace_init(void)
  1081. {
  1082. int rc;
  1083. ace_major = register_blkdev(ace_major, "xsysace");
  1084. if (ace_major <= 0) {
  1085. rc = -ENOMEM;
  1086. goto err_blk;
  1087. }
  1088. rc = ace_of_register();
  1089. if (rc)
  1090. goto err_of;
  1091. pr_debug("xsysace: registering platform binding\n");
  1092. rc = platform_driver_register(&ace_platform_driver);
  1093. if (rc)
  1094. goto err_plat;
  1095. pr_info("Xilinx SystemACE device driver, major=%i\n", ace_major);
  1096. return 0;
  1097. err_plat:
  1098. ace_of_unregister();
  1099. err_of:
  1100. unregister_blkdev(ace_major, "xsysace");
  1101. err_blk:
  1102. printk(KERN_ERR "xsysace: registration failed; err=%i\n", rc);
  1103. return rc;
  1104. }
  1105. static void __exit ace_exit(void)
  1106. {
  1107. pr_debug("Unregistering Xilinx SystemACE driver\n");
  1108. platform_driver_unregister(&ace_platform_driver);
  1109. ace_of_unregister();
  1110. unregister_blkdev(ace_major, "xsysace");
  1111. }
  1112. module_init(ace_init);
  1113. module_exit(ace_exit);