xsysace.c 34 KB

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