cycx_drv.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * cycx_drv.c Cyclom 2X Support Module.
  3. *
  4. * This module is a library of common hardware specific
  5. * functions used by the Cyclades Cyclom 2X sync card.
  6. *
  7. * Author: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  8. *
  9. * Copyright: (c) 1998-2003 Arnaldo Carvalho de Melo
  10. *
  11. * Based on sdladrv.c by Gene Kozin <genek@compuserve.com>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. * ============================================================================
  18. * 1999/11/11 acme set_current_state(TASK_INTERRUPTIBLE), code
  19. * cleanup
  20. * 1999/11/08 acme init_cyc2x deleted, doing nothing
  21. * 1999/11/06 acme back to read[bw], write[bw] and memcpy_to and
  22. * fromio to use dpmbase ioremaped
  23. * 1999/10/26 acme use isa_read[bw], isa_write[bw] & isa_memcpy_to
  24. * & fromio
  25. * 1999/10/23 acme cleanup to only supports cyclom2x: all the other
  26. * boards are no longer manufactured by cyclades,
  27. * if someone wants to support them... be my guest!
  28. * 1999/05/28 acme cycx_intack & cycx_intde gone for good
  29. * 1999/05/18 acme lots of unlogged work, submitting to Linus...
  30. * 1999/01/03 acme more judicious use of data types
  31. * 1999/01/03 acme judicious use of data types :>
  32. * cycx_inten trying to reset pending interrupts
  33. * from cyclom 2x - I think this isn't the way to
  34. * go, but for now...
  35. * 1999/01/02 acme cycx_intack ok, I think there's nothing to do
  36. * to ack an int in cycx_drv.c, only handle it in
  37. * cyx_isr (or in the other protocols: cyp_isr,
  38. * cyf_isr, when they get implemented.
  39. * Dec 31, 1998 acme cycx_data_boot & cycx_code_boot fixed, crossing
  40. * fingers to see x25_configure in cycx_x25.c
  41. * work... :)
  42. * Dec 26, 1998 acme load implementation fixed, seems to work! :)
  43. * cycx_2x_dpmbase_options with all the possible
  44. * DPM addresses (20).
  45. * cycx_intr implemented (test this!)
  46. * general code cleanup
  47. * Dec 8, 1998 Ivan Passos Cyclom-2X firmware load implementation.
  48. * Aug 8, 1998 acme Initial version.
  49. */
  50. #include <linux/init.h> /* __init */
  51. #include <linux/module.h>
  52. #include <linux/kernel.h> /* printk(), and other useful stuff */
  53. #include <linux/stddef.h> /* offsetof(), etc. */
  54. #include <linux/errno.h> /* return codes */
  55. #include <linux/sched.h> /* for jiffies, HZ, etc. */
  56. #include <linux/cycx_drv.h> /* API definitions */
  57. #include <linux/cycx_cfm.h> /* CYCX firmware module definitions */
  58. #include <linux/delay.h> /* udelay */
  59. #include <asm/io.h> /* read[wl], write[wl], ioremap, iounmap */
  60. #define MOD_VERSION 0
  61. #define MOD_RELEASE 6
  62. MODULE_AUTHOR("Arnaldo Carvalho de Melo");
  63. MODULE_DESCRIPTION("Cyclom 2x Sync Card Driver");
  64. MODULE_LICENSE("GPL");
  65. /* Hardware-specific functions */
  66. static int load_cyc2x(struct cycx_hw *hw, struct cycx_firmware *cfm, u32 len);
  67. static void cycx_bootcfg(struct cycx_hw *hw);
  68. static int reset_cyc2x(void __iomem *addr);
  69. static int detect_cyc2x(void __iomem *addr);
  70. /* Miscellaneous functions */
  71. static void delay_cycx(int sec);
  72. static int get_option_index(long *optlist, long optval);
  73. static u16 checksum(u8 *buf, u32 len);
  74. #define wait_cyc(addr) cycx_exec(addr + CMD_OFFSET)
  75. /* Global Data */
  76. /* private data */
  77. static char modname[] = "cycx_drv";
  78. static char fullname[] = "Cyclom 2X Support Module";
  79. static char copyright[] = "(c) 1998-2003 Arnaldo Carvalho de Melo "
  80. "<acme@conectiva.com.br>";
  81. /* Hardware configuration options.
  82. * These are arrays of configuration options used by verification routines.
  83. * The first element of each array is its size (i.e. number of options).
  84. */
  85. static long cyc2x_dpmbase_options[] = {
  86. 20,
  87. 0xA0000, 0xA4000, 0xA8000, 0xAC000, 0xB0000, 0xB4000, 0xB8000,
  88. 0xBC000, 0xC0000, 0xC4000, 0xC8000, 0xCC000, 0xD0000, 0xD4000,
  89. 0xD8000, 0xDC000, 0xE0000, 0xE4000, 0xE8000, 0xEC000
  90. };
  91. static long cycx_2x_irq_options[] = { 7, 3, 5, 9, 10, 11, 12, 15 };
  92. /* Kernel Loadable Module Entry Points */
  93. /* Module 'insert' entry point.
  94. * o print announcement
  95. * o initialize static data
  96. *
  97. * Return: 0 Ok
  98. * < 0 error.
  99. * Context: process */
  100. int __init cycx_drv_init(void)
  101. {
  102. printk(KERN_INFO "%s v%u.%u %s\n", fullname, MOD_VERSION, MOD_RELEASE,
  103. copyright);
  104. return 0;
  105. }
  106. /* Module 'remove' entry point.
  107. * o release all remaining system resources */
  108. void cycx_drv_cleanup(void)
  109. {
  110. }
  111. /* Kernel APIs */
  112. /* Set up adapter.
  113. * o detect adapter type
  114. * o verify hardware configuration options
  115. * o check for hardware conflicts
  116. * o set up adapter shared memory
  117. * o test adapter memory
  118. * o load firmware
  119. * Return: 0 ok.
  120. * < 0 error */
  121. EXPORT_SYMBOL(cycx_setup);
  122. int cycx_setup(struct cycx_hw *hw, void *cfm, u32 len, unsigned long dpmbase)
  123. {
  124. int err;
  125. /* Verify IRQ configuration options */
  126. if (!get_option_index(cycx_2x_irq_options, hw->irq)) {
  127. printk(KERN_ERR "%s: IRQ %d is invalid!\n", modname, hw->irq);
  128. return -EINVAL;
  129. }
  130. /* Setup adapter dual-port memory window and test memory */
  131. if (!dpmbase) {
  132. printk(KERN_ERR "%s: you must specify the dpm address!\n",
  133. modname);
  134. return -EINVAL;
  135. } else if (!get_option_index(cyc2x_dpmbase_options, dpmbase)) {
  136. printk(KERN_ERR "%s: memory address 0x%lX is invalid!\n",
  137. modname, dpmbase);
  138. return -EINVAL;
  139. }
  140. hw->dpmbase = ioremap(dpmbase, CYCX_WINDOWSIZE);
  141. hw->dpmsize = CYCX_WINDOWSIZE;
  142. if (!detect_cyc2x(hw->dpmbase)) {
  143. printk(KERN_ERR "%s: adapter Cyclom 2X not found at "
  144. "address 0x%lX!\n", modname, dpmbase);
  145. return -EINVAL;
  146. }
  147. printk(KERN_INFO "%s: found Cyclom 2X card at address 0x%lX.\n",
  148. modname, dpmbase);
  149. /* Load firmware. If loader fails then shut down adapter */
  150. err = load_cyc2x(hw, cfm, len);
  151. if (err)
  152. cycx_down(hw); /* shutdown adapter */
  153. return err;
  154. }
  155. EXPORT_SYMBOL(cycx_down);
  156. int cycx_down(struct cycx_hw *hw)
  157. {
  158. iounmap(hw->dpmbase);
  159. return 0;
  160. }
  161. /* Enable interrupt generation. */
  162. EXPORT_SYMBOL(cycx_inten);
  163. void cycx_inten(struct cycx_hw *hw)
  164. {
  165. writeb(0, hw->dpmbase);
  166. }
  167. /* Generate an interrupt to adapter's CPU. */
  168. EXPORT_SYMBOL(cycx_intr);
  169. void cycx_intr(struct cycx_hw *hw)
  170. {
  171. writew(0, hw->dpmbase + GEN_CYCX_INTR);
  172. }
  173. /* Execute Adapter Command.
  174. * o Set exec flag.
  175. * o Busy-wait until flag is reset. */
  176. EXPORT_SYMBOL(cycx_exec);
  177. int cycx_exec(void __iomem *addr)
  178. {
  179. u16 i = 0;
  180. /* wait till addr content is zeroed */
  181. while (readw(addr)) {
  182. udelay(1000);
  183. if (++i > 50)
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. /* Read absolute adapter memory.
  189. * Transfer data from adapter's memory to data buffer. */
  190. EXPORT_SYMBOL(cycx_peek);
  191. int cycx_peek(struct cycx_hw *hw, u32 addr, void *buf, u32 len)
  192. {
  193. if (len == 1)
  194. *(u8*)buf = readb(hw->dpmbase + addr);
  195. else
  196. memcpy_fromio(buf, hw->dpmbase + addr, len);
  197. return 0;
  198. }
  199. /* Write Absolute Adapter Memory.
  200. * Transfer data from data buffer to adapter's memory. */
  201. EXPORT_SYMBOL(cycx_poke);
  202. int cycx_poke(struct cycx_hw *hw, u32 addr, void *buf, u32 len)
  203. {
  204. if (len == 1)
  205. writeb(*(u8*)buf, hw->dpmbase + addr);
  206. else
  207. memcpy_toio(hw->dpmbase + addr, buf, len);
  208. return 0;
  209. }
  210. /* Hardware-Specific Functions */
  211. /* Load Aux Routines */
  212. /* Reset board hardware.
  213. return 1 if memory exists at addr and 0 if not. */
  214. static int memory_exists(void __iomem *addr)
  215. {
  216. int tries = 0;
  217. for (; tries < 3 ; tries++) {
  218. writew(TEST_PATTERN, addr + 0x10);
  219. if (readw(addr + 0x10) == TEST_PATTERN)
  220. if (readw(addr + 0x10) == TEST_PATTERN)
  221. return 1;
  222. delay_cycx(1);
  223. }
  224. return 0;
  225. }
  226. /* Load reset code. */
  227. static void reset_load(void __iomem *addr, u8 *buffer, u32 cnt)
  228. {
  229. void __iomem *pt_code = addr + RESET_OFFSET;
  230. u16 i; /*, j; */
  231. for (i = 0 ; i < cnt ; i++) {
  232. /* for (j = 0 ; j < 50 ; j++); Delay - FIXME busy waiting... */
  233. writeb(*buffer++, pt_code++);
  234. }
  235. }
  236. /* Load buffer using boot interface.
  237. * o copy data from buffer to Cyclom-X memory
  238. * o wait for reset code to copy it to right portion of memory */
  239. static int buffer_load(void __iomem *addr, u8 *buffer, u32 cnt)
  240. {
  241. memcpy_toio(addr + DATA_OFFSET, buffer, cnt);
  242. writew(GEN_BOOT_DAT, addr + CMD_OFFSET);
  243. return wait_cyc(addr);
  244. }
  245. /* Set up entry point and kick start Cyclom-X CPU. */
  246. static void cycx_start(void __iomem *addr)
  247. {
  248. /* put in 0x30 offset the jump instruction to the code entry point */
  249. writeb(0xea, addr + 0x30);
  250. writeb(0x00, addr + 0x31);
  251. writeb(0xc4, addr + 0x32);
  252. writeb(0x00, addr + 0x33);
  253. writeb(0x00, addr + 0x34);
  254. /* cmd to start executing code */
  255. writew(GEN_START, addr + CMD_OFFSET);
  256. }
  257. /* Load and boot reset code. */
  258. static void cycx_reset_boot(void __iomem *addr, u8 *code, u32 len)
  259. {
  260. void __iomem *pt_start = addr + START_OFFSET;
  261. writeb(0xea, pt_start++); /* jmp to f000:3f00 */
  262. writeb(0x00, pt_start++);
  263. writeb(0xfc, pt_start++);
  264. writeb(0x00, pt_start++);
  265. writeb(0xf0, pt_start);
  266. reset_load(addr, code, len);
  267. /* 80186 was in hold, go */
  268. writeb(0, addr + START_CPU);
  269. delay_cycx(1);
  270. }
  271. /* Load data.bin file through boot (reset) interface. */
  272. static int cycx_data_boot(void __iomem *addr, u8 *code, u32 len)
  273. {
  274. void __iomem *pt_boot_cmd = addr + CMD_OFFSET;
  275. u32 i;
  276. /* boot buffer lenght */
  277. writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
  278. writew(GEN_DEFPAR, pt_boot_cmd);
  279. if (wait_cyc(addr) < 0)
  280. return -1;
  281. writew(0, pt_boot_cmd + sizeof(u16));
  282. writew(0x4000, pt_boot_cmd + 2 * sizeof(u16));
  283. writew(GEN_SET_SEG, pt_boot_cmd);
  284. if (wait_cyc(addr) < 0)
  285. return -1;
  286. for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
  287. if (buffer_load(addr, code + i,
  288. min_t(u32, CFM_LOAD_BUFSZ, (len - i))) < 0) {
  289. printk(KERN_ERR "%s: Error !!\n", modname);
  290. return -1;
  291. }
  292. return 0;
  293. }
  294. /* Load code.bin file through boot (reset) interface. */
  295. static int cycx_code_boot(void __iomem *addr, u8 *code, u32 len)
  296. {
  297. void __iomem *pt_boot_cmd = addr + CMD_OFFSET;
  298. u32 i;
  299. /* boot buffer lenght */
  300. writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
  301. writew(GEN_DEFPAR, pt_boot_cmd);
  302. if (wait_cyc(addr) < 0)
  303. return -1;
  304. writew(0x0000, pt_boot_cmd + sizeof(u16));
  305. writew(0xc400, pt_boot_cmd + 2 * sizeof(u16));
  306. writew(GEN_SET_SEG, pt_boot_cmd);
  307. if (wait_cyc(addr) < 0)
  308. return -1;
  309. for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
  310. if (buffer_load(addr, code + i,
  311. min_t(u32, CFM_LOAD_BUFSZ, (len - i)))) {
  312. printk(KERN_ERR "%s: Error !!\n", modname);
  313. return -1;
  314. }
  315. return 0;
  316. }
  317. /* Load adapter from the memory image of the CYCX firmware module.
  318. * o verify firmware integrity and compatibility
  319. * o start adapter up */
  320. static int load_cyc2x(struct cycx_hw *hw, struct cycx_firmware *cfm, u32 len)
  321. {
  322. int i, j;
  323. struct cycx_fw_header *img_hdr;
  324. u8 *reset_image,
  325. *data_image,
  326. *code_image;
  327. void __iomem *pt_cycld = hw->dpmbase + 0x400;
  328. u16 cksum;
  329. /* Announce */
  330. printk(KERN_INFO "%s: firmware signature=\"%s\"\n", modname,
  331. cfm->signature);
  332. /* Verify firmware signature */
  333. if (strcmp(cfm->signature, CFM_SIGNATURE)) {
  334. printk(KERN_ERR "%s:load_cyc2x: not Cyclom-2X firmware!\n",
  335. modname);
  336. return -EINVAL;
  337. }
  338. printk(KERN_INFO "%s: firmware version=%u\n", modname, cfm->version);
  339. /* Verify firmware module format version */
  340. if (cfm->version != CFM_VERSION) {
  341. printk(KERN_ERR "%s:%s: firmware format %u rejected! "
  342. "Expecting %u.\n",
  343. modname, __FUNCTION__, cfm->version, CFM_VERSION);
  344. return -EINVAL;
  345. }
  346. /* Verify firmware module length and checksum */
  347. cksum = checksum((u8*)&cfm->info, sizeof(struct cycx_fw_info) +
  348. cfm->info.codesize);
  349. /*
  350. FIXME cfm->info.codesize is off by 2
  351. if (((len - sizeof(struct cycx_firmware) - 1) != cfm->info.codesize) ||
  352. */
  353. if (cksum != cfm->checksum) {
  354. printk(KERN_ERR "%s:%s: firmware corrupted!\n",
  355. modname, __FUNCTION__);
  356. printk(KERN_ERR " cdsize = 0x%x (expected 0x%lx)\n",
  357. len - (int)sizeof(struct cycx_firmware) - 1,
  358. cfm->info.codesize);
  359. printk(KERN_ERR " chksum = 0x%x (expected 0x%x)\n",
  360. cksum, cfm->checksum);
  361. return -EINVAL;
  362. }
  363. /* If everything is ok, set reset, data and code pointers */
  364. img_hdr = (struct cycx_fw_header *)&cfm->image;
  365. #ifdef FIRMWARE_DEBUG
  366. printk(KERN_INFO "%s:%s: image sizes\n", __FUNCTION__, modname);
  367. printk(KERN_INFO " reset=%lu\n", img_hdr->reset_size);
  368. printk(KERN_INFO " data=%lu\n", img_hdr->data_size);
  369. printk(KERN_INFO " code=%lu\n", img_hdr->code_size);
  370. #endif
  371. reset_image = ((u8 *)img_hdr) + sizeof(struct cycx_fw_header);
  372. data_image = reset_image + img_hdr->reset_size;
  373. code_image = data_image + img_hdr->data_size;
  374. /*---- Start load ----*/
  375. /* Announce */
  376. printk(KERN_INFO "%s: loading firmware %s (ID=%u)...\n", modname,
  377. cfm->descr[0] ? cfm->descr : "unknown firmware",
  378. cfm->info.codeid);
  379. for (i = 0 ; i < 5 ; i++) {
  380. /* Reset Cyclom hardware */
  381. if (!reset_cyc2x(hw->dpmbase)) {
  382. printk(KERN_ERR "%s: dpm problem or board not found\n",
  383. modname);
  384. return -EINVAL;
  385. }
  386. /* Load reset.bin */
  387. cycx_reset_boot(hw->dpmbase, reset_image, img_hdr->reset_size);
  388. /* reset is waiting for boot */
  389. writew(GEN_POWER_ON, pt_cycld);
  390. delay_cycx(1);
  391. for (j = 0 ; j < 3 ; j++)
  392. if (!readw(pt_cycld))
  393. goto reset_loaded;
  394. else
  395. delay_cycx(1);
  396. }
  397. printk(KERN_ERR "%s: reset not started.\n", modname);
  398. return -EINVAL;
  399. reset_loaded:
  400. /* Load data.bin */
  401. if (cycx_data_boot(hw->dpmbase, data_image, img_hdr->data_size)) {
  402. printk(KERN_ERR "%s: cannot load data file.\n", modname);
  403. return -EINVAL;
  404. }
  405. /* Load code.bin */
  406. if (cycx_code_boot(hw->dpmbase, code_image, img_hdr->code_size)) {
  407. printk(KERN_ERR "%s: cannot load code file.\n", modname);
  408. return -EINVAL;
  409. }
  410. /* Prepare boot-time configuration data */
  411. cycx_bootcfg(hw);
  412. /* kick-off CPU */
  413. cycx_start(hw->dpmbase);
  414. /* Arthur Ganzert's tip: wait a while after the firmware loading...
  415. seg abr 26 17:17:12 EST 1999 - acme */
  416. delay_cycx(7);
  417. printk(KERN_INFO "%s: firmware loaded!\n", modname);
  418. /* enable interrupts */
  419. cycx_inten(hw);
  420. return 0;
  421. }
  422. /* Prepare boot-time firmware configuration data.
  423. * o initialize configuration data area
  424. From async.doc - V_3.4.0 - 07/18/1994
  425. - As of now, only static buffers are available to the user.
  426. So, the bit VD_RXDIRC must be set in 'valid'. That means that user
  427. wants to use the static transmission and reception buffers. */
  428. static void cycx_bootcfg(struct cycx_hw *hw)
  429. {
  430. /* use fixed buffers */
  431. writeb(FIXED_BUFFERS, hw->dpmbase + CONF_OFFSET);
  432. }
  433. /* Detect Cyclom 2x adapter.
  434. * Following tests are used to detect Cyclom 2x adapter:
  435. * to be completed based on the tests done below
  436. * Return 1 if detected o.k. or 0 if failed.
  437. * Note: This test is destructive! Adapter will be left in shutdown
  438. * state after the test. */
  439. static int detect_cyc2x(void __iomem *addr)
  440. {
  441. reset_cyc2x(addr);
  442. return memory_exists(addr);
  443. }
  444. /* Miscellaneous */
  445. /* Get option's index into the options list.
  446. * Return option's index (1 .. N) or zero if option is invalid. */
  447. static int get_option_index(long *optlist, long optval)
  448. {
  449. int i = 1;
  450. for (; i <= optlist[0]; ++i)
  451. if (optlist[i] == optval)
  452. return i;
  453. return 0;
  454. }
  455. /* Reset adapter's CPU. */
  456. static int reset_cyc2x(void __iomem *addr)
  457. {
  458. writeb(0, addr + RST_ENABLE);
  459. delay_cycx(2);
  460. writeb(0, addr + RST_DISABLE);
  461. delay_cycx(2);
  462. return memory_exists(addr);
  463. }
  464. /* Delay */
  465. static void delay_cycx(int sec)
  466. {
  467. set_current_state(TASK_INTERRUPTIBLE);
  468. schedule_timeout(sec * HZ);
  469. }
  470. /* Calculate 16-bit CRC using CCITT polynomial. */
  471. static u16 checksum(u8 *buf, u32 len)
  472. {
  473. u16 crc = 0;
  474. u16 mask, flag;
  475. for (; len; --len, ++buf)
  476. for (mask = 0x80; mask; mask >>= 1) {
  477. flag = (crc & 0x8000);
  478. crc <<= 1;
  479. crc |= ((*buf & mask) ? 1 : 0);
  480. if (flag)
  481. crc ^= 0x1021;
  482. }
  483. return crc;
  484. }
  485. module_init(cycx_drv_init);
  486. module_exit(cycx_drv_cleanup);
  487. /* End */