cycx_drv.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:16k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  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-2000 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. /* Function Prototypes */
  66. /* Module entry points. These are called by the OS and must be public. */
  67. int init_module(void);
  68. void cleanup_module(void);
  69. /* Hardware-specific functions */
  70. static int load_cyc2x(cycxhw_t *hw, cfm_t *cfm, u32 len);
  71. static void cycx_bootcfg(cycxhw_t *hw);
  72. static int reset_cyc2x(u32 addr);
  73. static int detect_cyc2x(u32 addr);
  74. /* Miscellaneous functions */
  75. static void delay_cycx(int sec);
  76. static int get_option_index(u32 *optlist, u32 optval);
  77. static u16 checksum(u8 *buf, u32 len);
  78. #define wait_cyc(addr) cycx_exec(addr + CMD_OFFSET)
  79. #define cyc2x_readb(b) readb(b)
  80. #define cyc2x_readw(b) readw(b)
  81. #define cyc2x_writeb(b, addr) writeb(b, addr)
  82. #define cyc2x_writew(w, addr) writew(w, addr)
  83. #define cyc2x_memcpy_toio(addr, buf, len) memcpy_toio((addr), buf, len)
  84. #define cyc2x_memcpy_fromio(buf, addr, len) memcpy_fromio(buf, (addr), len)
  85. /* Global Data */
  86. /* private data */
  87. static char modname[] = "cycx_drv";
  88. static char fullname[] = "Cyclom 2X Support Module";
  89. static char copyright[] = "(c) 1998-2000 Arnaldo Carvalho de Melo "
  90.   "<acme@conectiva.com.br>";
  91. /* Hardware configuration options.
  92.  * These are arrays of configuration options used by verification routines.
  93.  * The first element of each array is its size (i.e. number of options).
  94.  */
  95. static u32 cyc2x_dpmbase_options[] =
  96. {
  97. 20,
  98. 0xA0000, 0xA4000, 0xA8000, 0xAC000, 0xB0000, 0xB4000, 0xB8000,
  99. 0xBC000, 0xC0000, 0xC4000, 0xC8000, 0xCC000, 0xD0000, 0xD4000,
  100. 0xD8000, 0xDC000, 0xE0000, 0xE4000, 0xE8000, 0xEC000
  101. };
  102. static u32 cycx_2x_irq_options[]  = { 7, 3, 5, 9, 10, 11, 12, 15 };
  103. /* Kernel Loadable Module Entry Points */
  104. /* Module 'insert' entry point.
  105.  * o print announcement
  106.  * o initialize static data
  107.  *
  108.  * Return: 0 Ok
  109.  * < 0 error.
  110.  * Context: process */
  111. int __init cycx_drv_init(void)
  112. {
  113. printk(KERN_INFO "%s v%u.%u %sn", fullname, MOD_VERSION, MOD_RELEASE,
  114.  copyright);
  115. return 0;
  116. }
  117. /* Module 'remove' entry point.
  118.  * o release all remaining system resources */
  119. void cycx_drv_cleanup(void)
  120. {
  121. }
  122. /* Kernel APIs */
  123. /* Set up adapter.
  124.  * o detect adapter type
  125.  * o verify hardware configuration options
  126.  * o check for hardware conflicts
  127.  * o set up adapter shared memory
  128.  * o test adapter memory
  129.  * o load firmware
  130.  * Return: 0 ok.
  131.  * < 0 error */
  132. EXPORT_SYMBOL(cycx_setup);
  133. int cycx_setup(cycxhw_t *hw, void *cfm, u32 len)
  134. {
  135. unsigned long dpmbase = hw->dpmbase;
  136. int err;
  137. /* Verify IRQ configuration options */
  138. if (!get_option_index(cycx_2x_irq_options, hw->irq)) {
  139. printk(KERN_ERR "%s: IRQ %d is illegal!n", modname, hw->irq);
  140. return -EINVAL;
  141. /* Setup adapter dual-port memory window and test memory */
  142. if (!hw->dpmbase) {
  143. printk(KERN_ERR "%s: you must specify the dpm address!n",
  144. modname);
  145.   return -EINVAL;
  146. } else if (!get_option_index(cyc2x_dpmbase_options, hw->dpmbase)) {
  147. printk(KERN_ERR "%s: memory address 0x%lX is illegal!n",
  148. modname, dpmbase);
  149. return -EINVAL;
  150. }
  151. hw->dpmbase = (u32)ioremap(dpmbase, CYCX_WINDOWSIZE);
  152. hw->dpmsize = CYCX_WINDOWSIZE;
  153. if (!detect_cyc2x(hw->dpmbase)) {
  154. printk(KERN_ERR "%s: adapter Cyclom 2X not found at "
  155. "address 0x%lX!n", modname, dpmbase);
  156. return -EINVAL;
  157. }
  158. printk(KERN_INFO "%s: found Cyclom 2X card at address 0x%lX.n",
  159.  modname, dpmbase);
  160. /* Load firmware. If loader fails then shut down adapter */
  161. err = load_cyc2x(hw, cfm, len);
  162. if (err)
  163. cycx_down(hw);         /* shutdown adapter */
  164. return err;
  165. EXPORT_SYMBOL(cycx_down);
  166. int cycx_down(cycxhw_t *hw)
  167. {
  168. iounmap((u32 *)hw->dpmbase);
  169. return 0;
  170. }
  171. /* Enable interrupt generation.  */
  172. EXPORT_SYMBOL(cycx_inten);
  173. void cycx_inten(cycxhw_t *hw)
  174. {
  175. cyc2x_writeb(0, hw->dpmbase);
  176. }
  177. /* Generate an interrupt to adapter's CPU. */
  178. EXPORT_SYMBOL(cycx_intr);
  179. void cycx_intr(cycxhw_t *hw)
  180. {
  181. cyc2x_writew(0, hw->dpmbase + GEN_CYCX_INTR);
  182. }
  183. /* Execute Adapter Command.
  184.  * o Set exec flag.
  185.  * o Busy-wait until flag is reset. */
  186. EXPORT_SYMBOL(cycx_exec);
  187. int cycx_exec(u32 addr)
  188. {
  189. u16 i = 0;
  190. /* wait till addr content is zeroed */
  191. while (cyc2x_readw(addr)) {
  192. udelay(1000);
  193. if (++i > 50)
  194. return -1;
  195. }
  196. return 0;
  197. }
  198. /* Read absolute adapter memory.
  199.  * Transfer data from adapter's memory to data buffer. */
  200. EXPORT_SYMBOL(cycx_peek);
  201. int cycx_peek(cycxhw_t *hw, u32 addr, void *buf, u32 len)
  202. {
  203. if (len == 1)
  204. *(u8*)buf = cyc2x_readb(hw->dpmbase + addr);
  205. else
  206. cyc2x_memcpy_fromio(buf, hw->dpmbase + addr, len);
  207. return 0;
  208. }
  209. /* Write Absolute Adapter Memory.
  210.  * Transfer data from data buffer to adapter's memory. */
  211. EXPORT_SYMBOL(cycx_poke);
  212. int cycx_poke(cycxhw_t *hw, u32 addr, void *buf, u32 len)
  213. {
  214. if (len == 1)
  215. cyc2x_writeb(*(u8*)buf, hw->dpmbase + addr);
  216. else
  217. cyc2x_memcpy_toio(hw->dpmbase + addr, buf, len);
  218. return 0;
  219. }
  220. /* Hardware-Specific Functions */
  221. /* Load Aux Routines */
  222. /* Reset board hardware.
  223.    return 1 if memory exists at addr and 0 if not. */
  224. static int memory_exists(u32 addr)
  225. {
  226. int tries = 0;
  227. for (; tries < 3 ; tries++) {
  228. cyc2x_writew(TEST_PATTERN, addr + 0x10);
  229. if (cyc2x_readw(addr + 0x10) == TEST_PATTERN)
  230. if (cyc2x_readw(addr + 0x10) == TEST_PATTERN)
  231. return 1;
  232. delay_cycx(1);
  233. }
  234. return 0;
  235. }
  236. /* Load reset code. */
  237. static void reset_load(u32 addr, u8 *buffer, u32 cnt)
  238. {
  239. u32 pt_code = addr + RESET_OFFSET;
  240. u16 i; /*, j; */
  241. for (i = 0 ; i < cnt ; i++) {
  242. /* for (j = 0 ; j < 50 ; j++); Delay - FIXME busy waiting... */
  243. cyc2x_writeb(*buffer++, pt_code++);
  244. }
  245. }
  246. /* Load buffer using boot interface.
  247.  * o copy data from buffer to Cyclom-X memory
  248.  * o wait for reset code to copy it to right portion of memory */
  249. static int buffer_load(u32 addr, u8 *buffer, u32 cnt)
  250. {
  251. cyc2x_memcpy_toio(addr + DATA_OFFSET, buffer, cnt);
  252. cyc2x_writew(GEN_BOOT_DAT, addr + CMD_OFFSET);
  253. return wait_cyc(addr);
  254. }
  255. /* Set up entry point and kick start Cyclom-X CPU. */
  256. static void cycx_start(u32 addr)
  257. {
  258. /* put in 0x30 offset the jump instruction to the code entry point */
  259. cyc2x_writeb(0xea, addr + 0x30);
  260. cyc2x_writeb(0x00, addr + 0x31);
  261. cyc2x_writeb(0xc4, addr + 0x32);
  262. cyc2x_writeb(0x00, addr + 0x33);
  263. cyc2x_writeb(0x00, addr + 0x34);
  264. /* cmd to start executing code */
  265. cyc2x_writew(GEN_START, addr + CMD_OFFSET);
  266. }         
  267. /* Load and boot reset code. */
  268. static void cycx_reset_boot(u32 addr, u8 *code, u32 len)
  269. {
  270. u32 pt_start = addr + START_OFFSET;
  271.         cyc2x_writeb(0xea, pt_start++); /* jmp to f000:3f00 */
  272.         cyc2x_writeb(0x00, pt_start++);
  273.         cyc2x_writeb(0xfc, pt_start++);
  274.         cyc2x_writeb(0x00, pt_start++);
  275.         cyc2x_writeb(0xf0, pt_start);
  276. reset_load(addr, code, len);
  277. /* 80186 was in hold, go */
  278. cyc2x_writeb(0, addr + START_CPU);
  279. delay_cycx(1);
  280. }
  281. /* Load data.bin file through boot (reset) interface. */
  282. static int cycx_data_boot(u32 addr, u8 *code, u32 len)
  283. {
  284. u32 pt_boot_cmd = addr + CMD_OFFSET;
  285. u32 i;
  286. /* boot buffer lenght */
  287. cyc2x_writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
  288. cyc2x_writew(GEN_DEFPAR, pt_boot_cmd);
  289. if (wait_cyc(addr) < 0)
  290. return -1;
  291. cyc2x_writew(0, pt_boot_cmd + sizeof(u16));
  292. cyc2x_writew(0x4000, pt_boot_cmd + 2 * sizeof(u16));
  293. cyc2x_writew(GEN_SET_SEG, pt_boot_cmd);
  294. if (wait_cyc(addr) < 0)
  295. return -1;
  296. for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
  297. if (buffer_load(addr, code + i,
  298. MIN(CFM_LOAD_BUFSZ, (len - i))) < 0) {
  299. printk(KERN_ERR "%s: Error !!n", modname);
  300. return -1;
  301. }
  302. return 0;
  303. }
  304. /* Load code.bin file through boot (reset) interface. */
  305. static int cycx_code_boot(u32 addr, u8 *code, u32 len)
  306. {
  307. u32 pt_boot_cmd = addr + CMD_OFFSET;
  308. u32 i;
  309. /* boot buffer lenght */
  310. cyc2x_writew(CFM_LOAD_BUFSZ, pt_boot_cmd + sizeof(u16));
  311. cyc2x_writew(GEN_DEFPAR, pt_boot_cmd);
  312. if (wait_cyc(addr) < 0)
  313. return -1;
  314. cyc2x_writew(0x0000, pt_boot_cmd + sizeof(u16));
  315. cyc2x_writew(0xc400, pt_boot_cmd + 2 * sizeof(u16));
  316. cyc2x_writew(GEN_SET_SEG, pt_boot_cmd);
  317. if (wait_cyc(addr) < 0)
  318. return -1;
  319. for (i = 0 ; i < len ; i += CFM_LOAD_BUFSZ)
  320. if (buffer_load(addr, code + i,MIN(CFM_LOAD_BUFSZ,(len - i)))) {
  321. printk(KERN_ERR "%s: Error !!n", modname);
  322. return -1;
  323. }
  324. return 0;
  325. }
  326. /* Load adapter from the memory image of the CYCX firmware module. 
  327.  * o verify firmware integrity and compatibility
  328.  * o start adapter up */
  329. static int load_cyc2x(cycxhw_t *hw, cfm_t *cfm, u32 len)
  330. {
  331. int i, j;
  332. cycx_header_t *img_hdr;
  333. u8 *reset_image,
  334.    *data_image,
  335.    *code_image;
  336. u32 pt_cycld = hw->dpmbase + 0x400;
  337. u16 cksum;
  338. /* Announce */
  339. printk(KERN_INFO "%s: firmware signature="%s"n", modname,
  340.     cfm->signature);
  341. /* Verify firmware signature */
  342. if (strcmp(cfm->signature, CFM_SIGNATURE)) {
  343. printk(KERN_ERR "%s:load_cyc2x: not Cyclom-2X firmware!n",
  344. modname);
  345. return -EINVAL;
  346. }
  347. printk(KERN_INFO "%s: firmware version=%un", modname, cfm->version);
  348. /* Verify firmware module format version */
  349. if (cfm->version != CFM_VERSION) {
  350. printk(KERN_ERR "%s:" __FUNCTION__ ": firmware format %u rejected! "
  351. "Expecting %u.n",
  352. modname, cfm->version, CFM_VERSION);
  353. return -EINVAL;
  354. }
  355. /* Verify firmware module length and checksum */
  356. cksum = checksum((u8*)&cfm->info, sizeof(cfm_info_t) +
  357.       cfm->info.codesize);
  358. /*
  359.         FIXME cfm->info.codesize is off by 2
  360. if (((len - sizeof(cfm_t) - 1) != cfm->info.codesize) ||
  361. */
  362. if (cksum != cfm->checksum) {
  363. printk(KERN_ERR "%s:" __FUNCTION__ ": firmware corrupted!n",
  364. modname);
  365. printk(KERN_ERR " cdsize = 0x%x (expected 0x%lx)n",
  366. len - sizeof(cfm_t) - 1, cfm->info.codesize);
  367.                 printk(KERN_ERR " chksum = 0x%x (expected 0x%x)n",
  368. cksum, cfm->checksum);
  369. return -EINVAL;
  370. }
  371. /* If everything is ok, set reset, data and code pointers */
  372. img_hdr = (cycx_header_t*)(((u8*)cfm) + sizeof(cfm_t) - 1);
  373. #ifdef FIRMWARE_DEBUG
  374. printk(KERN_INFO "%s:" __FUNCTION__ ": image sizesn", modname);
  375. printk(KERN_INFO " reset=%lun", img_hdr->reset_size);
  376. printk(KERN_INFO "  data=%lun", img_hdr->data_size);
  377. printk(KERN_INFO "  code=%lun", img_hdr->code_size);
  378. #endif
  379. reset_image = ((u8 *)img_hdr) + sizeof(cycx_header_t);
  380. data_image = reset_image + img_hdr->reset_size;
  381. code_image = data_image + img_hdr->data_size;
  382. /*---- Start load ----*/
  383.         /* Announce */
  384. printk(KERN_INFO "%s: loading firmware %s (ID=%u)...n", modname,
  385.  cfm->descr[0] ? cfm->descr : "unknown firmware",
  386.  cfm->info.codeid);
  387. for (i = 0 ; i < 5 ; i++) {
  388. /* Reset Cyclom hardware */
  389. if (!reset_cyc2x(hw->dpmbase)) {
  390. printk(KERN_ERR "%s: dpm problem or board not foundn",
  391. modname);
  392. return -EINVAL;
  393. }
  394. /* Load reset.bin */
  395.                 cycx_reset_boot(hw->dpmbase, reset_image, img_hdr->reset_size);
  396. /* reset is waiting for boot */
  397. cyc2x_writew(GEN_POWER_ON, pt_cycld);
  398. delay_cycx(1);
  399. for (j = 0 ; j < 3 ; j++)
  400. if (!cyc2x_readw(pt_cycld))
  401. goto reset_loaded;
  402. else
  403. delay_cycx(1);
  404. }
  405. printk(KERN_ERR "%s: reset not started.n", modname);
  406. return -EINVAL;
  407. reset_loaded:
  408. /* Load data.bin */
  409. if (cycx_data_boot(hw->dpmbase, data_image, img_hdr->data_size)) {
  410. printk(KERN_ERR "%s: cannot load data file.n", modname);
  411. return -EINVAL;
  412. }
  413. /* Load code.bin */
  414. if (cycx_code_boot(hw->dpmbase, code_image, img_hdr->code_size)) {
  415. printk(KERN_ERR "%s: cannot load code file.n", modname);
  416. return -EINVAL;
  417. }
  418. /* Prepare boot-time configuration data */
  419. cycx_bootcfg(hw);
  420. /* kick-off CPU */
  421. cycx_start(hw->dpmbase);
  422. /* Arthur Ganzert's tip: wait a while after the firmware loading...
  423.    seg abr 26 17:17:12 EST 1999 - acme */
  424. delay_cycx(7);
  425. printk(KERN_INFO "%s: firmware loaded!n", modname);
  426. /* enable interrupts */
  427.         cycx_inten(hw);
  428. return 0;
  429. }
  430. /* Prepare boot-time firmware configuration data.
  431.  * o initialize configuration data area
  432.    From async.doc - V_3.4.0 - 07/18/1994
  433.    - As of now, only static buffers are available to the user.
  434.      So, the bit VD_RXDIRC must be set in 'valid'. That means that user
  435.      wants to use the static transmission and reception buffers. */
  436. static void cycx_bootcfg(cycxhw_t *hw)
  437. {
  438. /* use fixed buffers */
  439. cyc2x_writeb(FIXED_BUFFERS, hw->dpmbase + CONF_OFFSET); 
  440. }
  441. /* Detect Cyclom 2x adapter.
  442.  * Following tests are used to detect Cyclom 2x adapter:
  443.  *       to be completed based on the tests done below
  444.  * Return 1 if detected o.k. or 0 if failed.
  445.  * Note: This test is destructive! Adapter will be left in shutdown
  446.  * state after the test. */
  447. static int detect_cyc2x(u32 addr)
  448. {
  449. reset_cyc2x(addr);
  450. return memory_exists(addr);
  451. }
  452. /* Miscellaneous */
  453. /* Get option's index into the options list.
  454.  * Return option's index (1 .. N) or zero if option is invalid. */
  455. static int get_option_index(u32 *optlist, u32 optval)
  456. {
  457. int i = 1;
  458. for (; i <= optlist[0]; ++i)
  459. if (optlist[i] == optval)
  460. return i;
  461. return 0;
  462. }
  463. /* Reset adapter's CPU. */
  464. static int reset_cyc2x(u32 addr)
  465. {
  466. cyc2x_writeb(0, addr + RST_ENABLE);
  467. delay_cycx(2);
  468. cyc2x_writeb(0, addr + RST_DISABLE);
  469. delay_cycx(2);
  470. return memory_exists(addr);
  471. }
  472. /* Delay */
  473. static void delay_cycx(int sec)
  474. {
  475. set_current_state(TASK_INTERRUPTIBLE);
  476. schedule_timeout(sec*HZ);
  477. }
  478. /* Calculate 16-bit CRC using CCITT polynomial. */
  479. static u16 checksum(u8 *buf, u32 len)
  480. {
  481. u16 crc = 0;
  482. u16 mask, flag;
  483. for (; len; --len, ++buf)
  484. for (mask = 0x80; mask; mask >>= 1) {
  485. flag = (crc & 0x8000);
  486. crc <<= 1;
  487. crc |= ((*buf & mask) ? 1 : 0);
  488. if (flag)
  489. crc ^= 0x1021;
  490. }
  491. return crc;
  492. }
  493. module_init(cycx_drv_init);
  494. module_exit(cycx_drv_cleanup);
  495. /* End */