io_interface_mux.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /* IO interface mux allocator for ETRAX100LX.
  2. * Copyright 2004-2007, Axis Communications AB
  3. */
  4. /* C.f. ETRAX100LX Designer's Reference chapter 19.9 */
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <linux/errno.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <arch/svinto.h>
  11. #include <asm/io.h>
  12. #include <arch/io_interface_mux.h>
  13. #include <arch/system.h>
  14. #define DBG(s)
  15. /* Macro to access ETRAX 100 registers */
  16. #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
  17. IO_STATE_(reg##_, field##_, _##val)
  18. enum io_if_group {
  19. group_a = (1<<0),
  20. group_b = (1<<1),
  21. group_c = (1<<2),
  22. group_d = (1<<3),
  23. group_e = (1<<4),
  24. group_f = (1<<5)
  25. };
  26. struct watcher
  27. {
  28. void (*notify)(const unsigned int gpio_in_available,
  29. const unsigned int gpio_out_available,
  30. const unsigned char pa_available,
  31. const unsigned char pb_available);
  32. struct watcher *next;
  33. };
  34. struct if_group
  35. {
  36. enum io_if_group group;
  37. /* name - the name of the group 'A' to 'F' */
  38. char *name;
  39. /* used - a bit mask of all pins in the group in the order listed
  40. * in the tables in 19.9.1 to 19.9.6. Note that no
  41. * distinction is made between in, out and in/out pins. */
  42. unsigned int used;
  43. };
  44. struct interface
  45. {
  46. enum cris_io_interface ioif;
  47. /* name - the name of the interface */
  48. char *name;
  49. /* groups - OR'ed together io_if_group flags describing what pin groups
  50. * the interface uses pins in. */
  51. unsigned char groups;
  52. /* used - set when the interface is allocated. */
  53. unsigned char used;
  54. char *owner;
  55. /* group_a through group_f - bit masks describing what pins in the
  56. * pin groups the interface uses. */
  57. unsigned int group_a;
  58. unsigned int group_b;
  59. unsigned int group_c;
  60. unsigned int group_d;
  61. unsigned int group_e;
  62. unsigned int group_f;
  63. /* gpio_g_in, gpio_g_out, gpio_b - bit masks telling what pins in the
  64. * GPIO ports the interface uses. This could be reconstucted using
  65. * the group_X masks and a table of what pins the GPIO ports use,
  66. * but that would be messy. */
  67. unsigned int gpio_g_in;
  68. unsigned int gpio_g_out;
  69. unsigned char gpio_b;
  70. };
  71. static struct if_group if_groups[6] = {
  72. {
  73. .group = group_a,
  74. .name = "A",
  75. .used = 0,
  76. },
  77. {
  78. .group = group_b,
  79. .name = "B",
  80. .used = 0,
  81. },
  82. {
  83. .group = group_c,
  84. .name = "C",
  85. .used = 0,
  86. },
  87. {
  88. .group = group_d,
  89. .name = "D",
  90. .used = 0,
  91. },
  92. {
  93. .group = group_e,
  94. .name = "E",
  95. .used = 0,
  96. },
  97. {
  98. .group = group_f,
  99. .name = "F",
  100. .used = 0,
  101. }
  102. };
  103. /* The order in the array must match the order of enum
  104. * cris_io_interface in io_interface_mux.h */
  105. static struct interface interfaces[] = {
  106. /* Begin Non-multiplexed interfaces */
  107. {
  108. .ioif = if_eth,
  109. .name = "ethernet",
  110. .groups = 0,
  111. .group_a = 0,
  112. .group_b = 0,
  113. .group_c = 0,
  114. .group_d = 0,
  115. .group_e = 0,
  116. .group_f = 0,
  117. .gpio_g_in = 0,
  118. .gpio_g_out = 0,
  119. .gpio_b = 0
  120. },
  121. {
  122. .ioif = if_serial_0,
  123. .name = "serial_0",
  124. .groups = 0,
  125. .group_a = 0,
  126. .group_b = 0,
  127. .group_c = 0,
  128. .group_d = 0,
  129. .group_e = 0,
  130. .group_f = 0,
  131. .gpio_g_in = 0,
  132. .gpio_g_out = 0,
  133. .gpio_b = 0
  134. },
  135. /* End Non-multiplexed interfaces */
  136. {
  137. .ioif = if_serial_1,
  138. .name = "serial_1",
  139. .groups = group_e,
  140. .group_a = 0,
  141. .group_b = 0,
  142. .group_c = 0,
  143. .group_d = 0,
  144. .group_e = 0x0f,
  145. .group_f = 0,
  146. .gpio_g_in = 0x00000000,
  147. .gpio_g_out = 0x00000000,
  148. .gpio_b = 0x00
  149. },
  150. {
  151. .ioif = if_serial_2,
  152. .name = "serial_2",
  153. .groups = group_b,
  154. .group_a = 0,
  155. .group_b = 0x0f,
  156. .group_c = 0,
  157. .group_d = 0,
  158. .group_e = 0,
  159. .group_f = 0,
  160. .gpio_g_in = 0x000000c0,
  161. .gpio_g_out = 0x000000c0,
  162. .gpio_b = 0x00
  163. },
  164. {
  165. .ioif = if_serial_3,
  166. .name = "serial_3",
  167. .groups = group_c,
  168. .group_a = 0,
  169. .group_b = 0,
  170. .group_c = 0x0f,
  171. .group_d = 0,
  172. .group_e = 0,
  173. .group_f = 0,
  174. .gpio_g_in = 0xc0000000,
  175. .gpio_g_out = 0xc0000000,
  176. .gpio_b = 0x00
  177. },
  178. {
  179. .ioif = if_sync_serial_1,
  180. .name = "sync_serial_1",
  181. .groups = group_e | group_f,
  182. .group_a = 0,
  183. .group_b = 0,
  184. .group_c = 0,
  185. .group_d = 0,
  186. .group_e = 0x0f,
  187. .group_f = 0x10,
  188. .gpio_g_in = 0x00000000,
  189. .gpio_g_out = 0x00000000,
  190. .gpio_b = 0x10
  191. },
  192. {
  193. .ioif = if_sync_serial_3,
  194. .name = "sync_serial_3",
  195. .groups = group_c | group_f,
  196. .group_a = 0,
  197. .group_b = 0,
  198. .group_c = 0x0f,
  199. .group_d = 0,
  200. .group_e = 0,
  201. .group_f = 0x80,
  202. .gpio_g_in = 0xc0000000,
  203. .gpio_g_out = 0xc0000000,
  204. .gpio_b = 0x80
  205. },
  206. {
  207. .ioif = if_shared_ram,
  208. .name = "shared_ram",
  209. .groups = group_a,
  210. .group_a = 0x7f8ff,
  211. .group_b = 0,
  212. .group_c = 0,
  213. .group_d = 0,
  214. .group_e = 0,
  215. .group_f = 0,
  216. .gpio_g_in = 0x0000ff3e,
  217. .gpio_g_out = 0x0000ff38,
  218. .gpio_b = 0x00
  219. },
  220. {
  221. .ioif = if_shared_ram_w,
  222. .name = "shared_ram_w",
  223. .groups = group_a | group_d,
  224. .group_a = 0x7f8ff,
  225. .group_b = 0,
  226. .group_c = 0,
  227. .group_d = 0xff,
  228. .group_e = 0,
  229. .group_f = 0,
  230. .gpio_g_in = 0x00ffff3e,
  231. .gpio_g_out = 0x00ffff38,
  232. .gpio_b = 0x00
  233. },
  234. {
  235. .ioif = if_par_0,
  236. .name = "par_0",
  237. .groups = group_a,
  238. .group_a = 0x7fbff,
  239. .group_b = 0,
  240. .group_c = 0,
  241. .group_d = 0,
  242. .group_e = 0,
  243. .group_f = 0,
  244. .gpio_g_in = 0x0000ff3e,
  245. .gpio_g_out = 0x0000ff3e,
  246. .gpio_b = 0x00
  247. },
  248. {
  249. .ioif = if_par_1,
  250. .name = "par_1",
  251. .groups = group_d,
  252. .group_a = 0,
  253. .group_b = 0,
  254. .group_c = 0,
  255. .group_d = 0x7feff,
  256. .group_e = 0,
  257. .group_f = 0,
  258. .gpio_g_in = 0x3eff0000,
  259. .gpio_g_out = 0x3eff0000,
  260. .gpio_b = 0x00
  261. },
  262. {
  263. .ioif = if_par_w,
  264. .name = "par_w",
  265. .groups = group_a | group_d,
  266. .group_a = 0x7fbff,
  267. .group_b = 0,
  268. .group_c = 0,
  269. .group_d = 0xff,
  270. .group_e = 0,
  271. .group_f = 0,
  272. .gpio_g_in = 0x00ffff3e,
  273. .gpio_g_out = 0x00ffff3e,
  274. .gpio_b = 0x00
  275. },
  276. {
  277. .ioif = if_scsi8_0,
  278. .name = "scsi8_0",
  279. .groups = group_a | group_b | group_f,
  280. .group_a = 0x7ffff,
  281. .group_b = 0x0f,
  282. .group_c = 0,
  283. .group_d = 0,
  284. .group_e = 0,
  285. .group_f = 0x10,
  286. .gpio_g_in = 0x0000ffff,
  287. .gpio_g_out = 0x0000ffff,
  288. .gpio_b = 0x10
  289. },
  290. {
  291. .ioif = if_scsi8_1,
  292. .name = "scsi8_1",
  293. .groups = group_c | group_d | group_f,
  294. .group_a = 0,
  295. .group_b = 0,
  296. .group_c = 0x0f,
  297. .group_d = 0x7ffff,
  298. .group_e = 0,
  299. .group_f = 0x80,
  300. .gpio_g_in = 0xffff0000,
  301. .gpio_g_out = 0xffff0000,
  302. .gpio_b = 0x80
  303. },
  304. {
  305. .ioif = if_scsi_w,
  306. .name = "scsi_w",
  307. .groups = group_a | group_b | group_d | group_f,
  308. .group_a = 0x7ffff,
  309. .group_b = 0x0f,
  310. .group_c = 0,
  311. .group_d = 0x601ff,
  312. .group_e = 0,
  313. .group_f = 0x90,
  314. .gpio_g_in = 0x01ffffff,
  315. .gpio_g_out = 0x07ffffff,
  316. .gpio_b = 0x80
  317. },
  318. {
  319. .ioif = if_ata,
  320. .name = "ata",
  321. .groups = group_a | group_b | group_c | group_d,
  322. .group_a = 0x7ffff,
  323. .group_b = 0x0f,
  324. .group_c = 0x0f,
  325. .group_d = 0x7cfff,
  326. .group_e = 0,
  327. .group_f = 0,
  328. .gpio_g_in = 0xf9ffffff,
  329. .gpio_g_out = 0xffffffff,
  330. .gpio_b = 0x80
  331. },
  332. {
  333. .ioif = if_csp,
  334. .name = "csp",
  335. .groups = group_f,
  336. .group_a = 0,
  337. .group_b = 0,
  338. .group_c = 0,
  339. .group_d = 0,
  340. .group_e = 0,
  341. .group_f = 0xfc,
  342. .gpio_g_in = 0x00000000,
  343. .gpio_g_out = 0x00000000,
  344. .gpio_b = 0xfc
  345. },
  346. {
  347. .ioif = if_i2c,
  348. .name = "i2c",
  349. .groups = group_f,
  350. .group_a = 0,
  351. .group_b = 0,
  352. .group_c = 0,
  353. .group_d = 0,
  354. .group_e = 0,
  355. .group_f = 0x03,
  356. .gpio_g_in = 0x00000000,
  357. .gpio_g_out = 0x00000000,
  358. .gpio_b = 0x03
  359. },
  360. {
  361. .ioif = if_usb_1,
  362. .name = "usb_1",
  363. .groups = group_e | group_f,
  364. .group_a = 0,
  365. .group_b = 0,
  366. .group_c = 0,
  367. .group_d = 0,
  368. .group_e = 0x0f,
  369. .group_f = 0x2c,
  370. .gpio_g_in = 0x00000000,
  371. .gpio_g_out = 0x00000000,
  372. .gpio_b = 0x2c
  373. },
  374. {
  375. .ioif = if_usb_2,
  376. .name = "usb_2",
  377. .groups = group_d,
  378. .group_a = 0,
  379. .group_b = 0,
  380. .group_c = 0,
  381. .group_d = 0,
  382. .group_e = 0x33e00,
  383. .group_f = 0,
  384. .gpio_g_in = 0x3e000000,
  385. .gpio_g_out = 0x0c000000,
  386. .gpio_b = 0x00
  387. },
  388. /* GPIO pins */
  389. {
  390. .ioif = if_gpio_grp_a,
  391. .name = "gpio_a",
  392. .groups = group_a,
  393. .group_a = 0,
  394. .group_b = 0,
  395. .group_c = 0,
  396. .group_d = 0,
  397. .group_e = 0,
  398. .group_f = 0,
  399. .gpio_g_in = 0x0000ff3f,
  400. .gpio_g_out = 0x0000ff3f,
  401. .gpio_b = 0x00
  402. },
  403. {
  404. .ioif = if_gpio_grp_b,
  405. .name = "gpio_b",
  406. .groups = group_b,
  407. .group_a = 0,
  408. .group_b = 0,
  409. .group_c = 0,
  410. .group_d = 0,
  411. .group_e = 0,
  412. .group_f = 0,
  413. .gpio_g_in = 0x000000c0,
  414. .gpio_g_out = 0x000000c0,
  415. .gpio_b = 0x00
  416. },
  417. {
  418. .ioif = if_gpio_grp_c,
  419. .name = "gpio_c",
  420. .groups = group_c,
  421. .group_a = 0,
  422. .group_b = 0,
  423. .group_c = 0,
  424. .group_d = 0,
  425. .group_e = 0,
  426. .group_f = 0,
  427. .gpio_g_in = 0xc0000000,
  428. .gpio_g_out = 0xc0000000,
  429. .gpio_b = 0x00
  430. },
  431. {
  432. .ioif = if_gpio_grp_d,
  433. .name = "gpio_d",
  434. .groups = group_d,
  435. .group_a = 0,
  436. .group_b = 0,
  437. .group_c = 0,
  438. .group_d = 0,
  439. .group_e = 0,
  440. .group_f = 0,
  441. .gpio_g_in = 0x3fff0000,
  442. .gpio_g_out = 0x3fff0000,
  443. .gpio_b = 0x00
  444. },
  445. {
  446. .ioif = if_gpio_grp_e,
  447. .name = "gpio_e",
  448. .groups = group_e,
  449. .group_a = 0,
  450. .group_b = 0,
  451. .group_c = 0,
  452. .group_d = 0,
  453. .group_e = 0,
  454. .group_f = 0,
  455. .gpio_g_in = 0x00000000,
  456. .gpio_g_out = 0x00000000,
  457. .gpio_b = 0x00
  458. },
  459. {
  460. .ioif = if_gpio_grp_f,
  461. .name = "gpio_f",
  462. .groups = group_f,
  463. .group_a = 0,
  464. .group_b = 0,
  465. .group_c = 0,
  466. .group_d = 0,
  467. .group_e = 0,
  468. .group_f = 0,
  469. .gpio_g_in = 0x00000000,
  470. .gpio_g_out = 0x00000000,
  471. .gpio_b = 0xff
  472. }
  473. /* Array end */
  474. };
  475. static struct watcher *watchers = NULL;
  476. /* The pins that are free to use in the GPIO ports. */
  477. static unsigned int gpio_in_pins = 0xffffffff;
  478. static unsigned int gpio_out_pins = 0xffffffff;
  479. static unsigned char gpio_pb_pins = 0xff;
  480. static unsigned char gpio_pa_pins = 0xff;
  481. /* Identifiers for the owners of the GPIO pins. */
  482. static enum cris_io_interface gpio_pa_owners[8];
  483. static enum cris_io_interface gpio_pb_owners[8];
  484. static enum cris_io_interface gpio_pg_owners[32];
  485. static int cris_io_interface_init(void);
  486. static unsigned char clear_group_from_set(const unsigned char groups, struct if_group *group)
  487. {
  488. return (groups & ~group->group);
  489. }
  490. static struct if_group *get_group(const unsigned char groups)
  491. {
  492. int i;
  493. for (i = 0; i < ARRAY_SIZE(if_groups); i++) {
  494. if (groups & if_groups[i].group) {
  495. return &if_groups[i];
  496. }
  497. }
  498. return NULL;
  499. }
  500. static void notify_watchers(void)
  501. {
  502. struct watcher *w = watchers;
  503. DBG(printk("io_interface_mux: notifying watchers\n"));
  504. while (NULL != w) {
  505. w->notify((const unsigned int)gpio_in_pins,
  506. (const unsigned int)gpio_out_pins,
  507. (const unsigned char)gpio_pa_pins,
  508. (const unsigned char)gpio_pb_pins);
  509. w = w->next;
  510. }
  511. }
  512. int cris_request_io_interface(enum cris_io_interface ioif, const char *device_id)
  513. {
  514. int set_gen_config = 0;
  515. int set_gen_config_ii = 0;
  516. unsigned long int gens;
  517. unsigned long int gens_ii;
  518. struct if_group *grp;
  519. unsigned char group_set;
  520. unsigned long flags;
  521. int res = 0;
  522. (void)cris_io_interface_init();
  523. DBG(printk("cris_request_io_interface(%d, \"%s\")\n", ioif, device_id));
  524. if ((ioif >= if_max_interfaces) || (ioif < 0)) {
  525. printk(KERN_CRIT "cris_request_io_interface: Bad interface "
  526. "%u submitted for %s\n",
  527. ioif,
  528. device_id);
  529. return -EINVAL;
  530. }
  531. local_irq_save(flags);
  532. if (interfaces[ioif].used) {
  533. printk(KERN_CRIT "cris_io_interface: Cannot allocate interface "
  534. "%s for %s, in use by %s\n",
  535. interfaces[ioif].name,
  536. device_id,
  537. interfaces[ioif].owner);
  538. res = -EBUSY;
  539. goto exit;
  540. }
  541. /* Check that all required pins in the used groups are free
  542. * before allocating. */
  543. group_set = interfaces[ioif].groups;
  544. while (NULL != (grp = get_group(group_set))) {
  545. unsigned int if_group_use = 0;
  546. switch (grp->group) {
  547. case group_a:
  548. if_group_use = interfaces[ioif].group_a;
  549. break;
  550. case group_b:
  551. if_group_use = interfaces[ioif].group_b;
  552. break;
  553. case group_c:
  554. if_group_use = interfaces[ioif].group_c;
  555. break;
  556. case group_d:
  557. if_group_use = interfaces[ioif].group_d;
  558. break;
  559. case group_e:
  560. if_group_use = interfaces[ioif].group_e;
  561. break;
  562. case group_f:
  563. if_group_use = interfaces[ioif].group_f;
  564. break;
  565. default:
  566. BUG_ON(1);
  567. }
  568. if (if_group_use & grp->used) {
  569. printk(KERN_INFO "cris_request_io_interface: group "
  570. "%s needed by %s not available\n",
  571. grp->name, interfaces[ioif].name);
  572. res = -EBUSY;
  573. goto exit;
  574. }
  575. group_set = clear_group_from_set(group_set, grp);
  576. }
  577. /* Are the required GPIO pins available too? */
  578. if (((interfaces[ioif].gpio_g_in & gpio_in_pins) !=
  579. interfaces[ioif].gpio_g_in) ||
  580. ((interfaces[ioif].gpio_g_out & gpio_out_pins) !=
  581. interfaces[ioif].gpio_g_out) ||
  582. ((interfaces[ioif].gpio_b & gpio_pb_pins) !=
  583. interfaces[ioif].gpio_b)) {
  584. printk(KERN_CRIT "cris_request_io_interface: Could not get "
  585. "required pins for interface %u\n", ioif);
  586. res = -EBUSY;
  587. goto exit;
  588. }
  589. /* Check which registers need to be reconfigured. */
  590. gens = genconfig_shadow;
  591. gens_ii = gen_config_ii_shadow;
  592. set_gen_config = 1;
  593. switch (ioif)
  594. {
  595. /* Begin Non-multiplexed interfaces */
  596. case if_eth:
  597. /* fall through */
  598. case if_serial_0:
  599. set_gen_config = 0;
  600. break;
  601. /* End Non-multiplexed interfaces */
  602. case if_serial_1:
  603. set_gen_config_ii = 1;
  604. SETS(gens_ii, R_GEN_CONFIG_II, sermode1, async);
  605. break;
  606. case if_serial_2:
  607. SETS(gens, R_GEN_CONFIG, ser2, select);
  608. break;
  609. case if_serial_3:
  610. SETS(gens, R_GEN_CONFIG, ser3, select);
  611. set_gen_config_ii = 1;
  612. SETS(gens_ii, R_GEN_CONFIG_II, sermode3, async);
  613. break;
  614. case if_sync_serial_1:
  615. set_gen_config_ii = 1;
  616. SETS(gens_ii, R_GEN_CONFIG_II, sermode1, sync);
  617. break;
  618. case if_sync_serial_3:
  619. SETS(gens, R_GEN_CONFIG, ser3, select);
  620. set_gen_config_ii = 1;
  621. SETS(gens_ii, R_GEN_CONFIG_II, sermode3, sync);
  622. break;
  623. case if_shared_ram:
  624. SETS(gens, R_GEN_CONFIG, mio, select);
  625. break;
  626. case if_shared_ram_w:
  627. SETS(gens, R_GEN_CONFIG, mio_w, select);
  628. break;
  629. case if_par_0:
  630. SETS(gens, R_GEN_CONFIG, par0, select);
  631. break;
  632. case if_par_1:
  633. SETS(gens, R_GEN_CONFIG, par1, select);
  634. break;
  635. case if_par_w:
  636. SETS(gens, R_GEN_CONFIG, par0, select);
  637. SETS(gens, R_GEN_CONFIG, par_w, select);
  638. break;
  639. case if_scsi8_0:
  640. SETS(gens, R_GEN_CONFIG, scsi0, select);
  641. break;
  642. case if_scsi8_1:
  643. SETS(gens, R_GEN_CONFIG, scsi1, select);
  644. break;
  645. case if_scsi_w:
  646. SETS(gens, R_GEN_CONFIG, scsi0, select);
  647. SETS(gens, R_GEN_CONFIG, scsi0w, select);
  648. break;
  649. case if_ata:
  650. SETS(gens, R_GEN_CONFIG, ata, select);
  651. break;
  652. case if_csp:
  653. /* fall through */
  654. case if_i2c:
  655. set_gen_config = 0;
  656. break;
  657. case if_usb_1:
  658. SETS(gens, R_GEN_CONFIG, usb1, select);
  659. break;
  660. case if_usb_2:
  661. SETS(gens, R_GEN_CONFIG, usb2, select);
  662. break;
  663. case if_gpio_grp_a:
  664. /* GPIO groups are only accounted, don't do configuration changes. */
  665. /* fall through */
  666. case if_gpio_grp_b:
  667. /* fall through */
  668. case if_gpio_grp_c:
  669. /* fall through */
  670. case if_gpio_grp_d:
  671. /* fall through */
  672. case if_gpio_grp_e:
  673. /* fall through */
  674. case if_gpio_grp_f:
  675. set_gen_config = 0;
  676. break;
  677. default:
  678. printk(KERN_INFO "cris_request_io_interface: Bad interface "
  679. "%u submitted for %s\n",
  680. ioif, device_id);
  681. res = -EBUSY;
  682. goto exit;
  683. }
  684. /* All needed I/O pins and pin groups are free, allocate. */
  685. group_set = interfaces[ioif].groups;
  686. while (NULL != (grp = get_group(group_set))) {
  687. unsigned int if_group_use = 0;
  688. switch (grp->group) {
  689. case group_a:
  690. if_group_use = interfaces[ioif].group_a;
  691. break;
  692. case group_b:
  693. if_group_use = interfaces[ioif].group_b;
  694. break;
  695. case group_c:
  696. if_group_use = interfaces[ioif].group_c;
  697. break;
  698. case group_d:
  699. if_group_use = interfaces[ioif].group_d;
  700. break;
  701. case group_e:
  702. if_group_use = interfaces[ioif].group_e;
  703. break;
  704. case group_f:
  705. if_group_use = interfaces[ioif].group_f;
  706. break;
  707. default:
  708. BUG_ON(1);
  709. }
  710. grp->used |= if_group_use;
  711. group_set = clear_group_from_set(group_set, grp);
  712. }
  713. interfaces[ioif].used = 1;
  714. interfaces[ioif].owner = (char*)device_id;
  715. if (set_gen_config) {
  716. volatile int i;
  717. genconfig_shadow = gens;
  718. *R_GEN_CONFIG = genconfig_shadow;
  719. /* Wait 12 cycles before doing any DMA command */
  720. for(i = 6; i > 0; i--)
  721. nop();
  722. }
  723. if (set_gen_config_ii) {
  724. gen_config_ii_shadow = gens_ii;
  725. *R_GEN_CONFIG_II = gen_config_ii_shadow;
  726. }
  727. DBG(printk(KERN_DEBUG "GPIO pins: available before: "
  728. "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  729. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  730. DBG(printk(KERN_DEBUG
  731. "grabbing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  732. interfaces[ioif].gpio_g_in,
  733. interfaces[ioif].gpio_g_out,
  734. interfaces[ioif].gpio_b));
  735. gpio_in_pins &= ~interfaces[ioif].gpio_g_in;
  736. gpio_out_pins &= ~interfaces[ioif].gpio_g_out;
  737. gpio_pb_pins &= ~interfaces[ioif].gpio_b;
  738. DBG(printk(KERN_DEBUG "GPIO pins: available after: "
  739. "g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  740. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  741. exit:
  742. local_irq_restore(flags);
  743. if (res == 0)
  744. notify_watchers();
  745. return res;
  746. }
  747. void cris_free_io_interface(enum cris_io_interface ioif)
  748. {
  749. struct if_group *grp;
  750. unsigned char group_set;
  751. unsigned long flags;
  752. (void)cris_io_interface_init();
  753. if ((ioif >= if_max_interfaces) || (ioif < 0)) {
  754. printk(KERN_CRIT "cris_free_io_interface: Bad interface %u\n",
  755. ioif);
  756. return;
  757. }
  758. local_irq_save(flags);
  759. if (!interfaces[ioif].used) {
  760. printk(KERN_CRIT "cris_free_io_interface: Freeing free interface %u\n",
  761. ioif);
  762. local_irq_restore(flags);
  763. return;
  764. }
  765. group_set = interfaces[ioif].groups;
  766. while (NULL != (grp = get_group(group_set))) {
  767. unsigned int if_group_use = 0;
  768. switch (grp->group) {
  769. case group_a:
  770. if_group_use = interfaces[ioif].group_a;
  771. break;
  772. case group_b:
  773. if_group_use = interfaces[ioif].group_b;
  774. break;
  775. case group_c:
  776. if_group_use = interfaces[ioif].group_c;
  777. break;
  778. case group_d:
  779. if_group_use = interfaces[ioif].group_d;
  780. break;
  781. case group_e:
  782. if_group_use = interfaces[ioif].group_e;
  783. break;
  784. case group_f:
  785. if_group_use = interfaces[ioif].group_f;
  786. break;
  787. default:
  788. BUG_ON(1);
  789. }
  790. if ((grp->used & if_group_use) != if_group_use)
  791. BUG_ON(1);
  792. grp->used = grp->used & ~if_group_use;
  793. group_set = clear_group_from_set(group_set, grp);
  794. }
  795. interfaces[ioif].used = 0;
  796. interfaces[ioif].owner = NULL;
  797. DBG(printk("GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  798. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  799. DBG(printk("freeing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  800. interfaces[ioif].gpio_g_in,
  801. interfaces[ioif].gpio_g_out,
  802. interfaces[ioif].gpio_b));
  803. gpio_in_pins |= interfaces[ioif].gpio_g_in;
  804. gpio_out_pins |= interfaces[ioif].gpio_g_out;
  805. gpio_pb_pins |= interfaces[ioif].gpio_b;
  806. DBG(printk("GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  807. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  808. local_irq_restore(flags);
  809. notify_watchers();
  810. }
  811. /* Create a bitmask from bit 0 (inclusive) to bit stop_bit
  812. (non-inclusive). stop_bit == 0 returns 0x0 */
  813. static inline unsigned int create_mask(const unsigned stop_bit)
  814. {
  815. /* Avoid overflow */
  816. if (stop_bit >= 32) {
  817. return 0xffffffff;
  818. }
  819. return (1<<stop_bit)-1;
  820. }
  821. /* port can be 'a', 'b' or 'g' */
  822. int cris_io_interface_allocate_pins(const enum cris_io_interface ioif,
  823. const char port,
  824. const unsigned start_bit,
  825. const unsigned stop_bit)
  826. {
  827. unsigned int i;
  828. unsigned int mask = 0;
  829. unsigned int tmp_mask;
  830. unsigned long int flags;
  831. enum cris_io_interface *owners;
  832. (void)cris_io_interface_init();
  833. DBG(printk("cris_io_interface_allocate_pins: if=%d port=%c start=%u stop=%u\n",
  834. ioif, port, start_bit, stop_bit));
  835. if (!((start_bit <= stop_bit) &&
  836. ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
  837. ((port == 'g') && (stop_bit < 32))))) {
  838. return -EINVAL;
  839. }
  840. mask = create_mask(stop_bit + 1);
  841. tmp_mask = create_mask(start_bit);
  842. mask &= ~tmp_mask;
  843. DBG(printk("cris_io_interface_allocate_pins: port=%c start=%u stop=%u mask=0x%08x\n",
  844. port, start_bit, stop_bit, mask));
  845. local_irq_save(flags);
  846. switch (port) {
  847. case 'a':
  848. if ((gpio_pa_pins & mask) != mask) {
  849. local_irq_restore(flags);
  850. return -EBUSY;
  851. }
  852. owners = gpio_pa_owners;
  853. gpio_pa_pins &= ~mask;
  854. break;
  855. case 'b':
  856. if ((gpio_pb_pins & mask) != mask) {
  857. local_irq_restore(flags);
  858. return -EBUSY;
  859. }
  860. owners = gpio_pb_owners;
  861. gpio_pb_pins &= ~mask;
  862. break;
  863. case 'g':
  864. if (((gpio_in_pins & mask) != mask) ||
  865. ((gpio_out_pins & mask) != mask)) {
  866. local_irq_restore(flags);
  867. return -EBUSY;
  868. }
  869. owners = gpio_pg_owners;
  870. gpio_in_pins &= ~mask;
  871. gpio_out_pins &= ~mask;
  872. break;
  873. default:
  874. local_irq_restore(flags);
  875. return -EINVAL;
  876. }
  877. for (i = start_bit; i <= stop_bit; i++) {
  878. owners[i] = ioif;
  879. }
  880. local_irq_restore(flags);
  881. notify_watchers();
  882. return 0;
  883. }
  884. /* port can be 'a', 'b' or 'g' */
  885. int cris_io_interface_free_pins(const enum cris_io_interface ioif,
  886. const char port,
  887. const unsigned start_bit,
  888. const unsigned stop_bit)
  889. {
  890. unsigned int i;
  891. unsigned int mask = 0;
  892. unsigned int tmp_mask;
  893. unsigned long int flags;
  894. enum cris_io_interface *owners;
  895. (void)cris_io_interface_init();
  896. if (!((start_bit <= stop_bit) &&
  897. ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
  898. ((port == 'g') && (stop_bit < 32))))) {
  899. return -EINVAL;
  900. }
  901. mask = create_mask(stop_bit + 1);
  902. tmp_mask = create_mask(start_bit);
  903. mask &= ~tmp_mask;
  904. DBG(printk("cris_io_interface_free_pins: port=%c start=%u stop=%u mask=0x%08x\n",
  905. port, start_bit, stop_bit, mask));
  906. local_irq_save(flags);
  907. switch (port) {
  908. case 'a':
  909. if ((~gpio_pa_pins & mask) != mask) {
  910. local_irq_restore(flags);
  911. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  912. }
  913. owners = gpio_pa_owners;
  914. break;
  915. case 'b':
  916. if ((~gpio_pb_pins & mask) != mask) {
  917. local_irq_restore(flags);
  918. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  919. }
  920. owners = gpio_pb_owners;
  921. break;
  922. case 'g':
  923. if (((~gpio_in_pins & mask) != mask) ||
  924. ((~gpio_out_pins & mask) != mask)) {
  925. local_irq_restore(flags);
  926. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  927. }
  928. owners = gpio_pg_owners;
  929. break;
  930. default:
  931. owners = NULL; /* Cannot happen. Shut up, gcc! */
  932. }
  933. for (i = start_bit; i <= stop_bit; i++) {
  934. if (owners[i] != ioif) {
  935. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing unowned pins");
  936. }
  937. }
  938. /* All was ok, change data. */
  939. switch (port) {
  940. case 'a':
  941. gpio_pa_pins |= mask;
  942. break;
  943. case 'b':
  944. gpio_pb_pins |= mask;
  945. break;
  946. case 'g':
  947. gpio_in_pins |= mask;
  948. gpio_out_pins |= mask;
  949. break;
  950. }
  951. for (i = start_bit; i <= stop_bit; i++) {
  952. owners[i] = if_unclaimed;
  953. }
  954. local_irq_restore(flags);
  955. notify_watchers();
  956. return 0;
  957. }
  958. int cris_io_interface_register_watcher(void (*notify)(const unsigned int gpio_in_available,
  959. const unsigned int gpio_out_available,
  960. const unsigned char pa_available,
  961. const unsigned char pb_available))
  962. {
  963. struct watcher *w;
  964. (void)cris_io_interface_init();
  965. if (NULL == notify) {
  966. return -EINVAL;
  967. }
  968. w = kmalloc(sizeof(*w), GFP_KERNEL);
  969. if (!w) {
  970. return -ENOMEM;
  971. }
  972. w->notify = notify;
  973. w->next = watchers;
  974. watchers = w;
  975. w->notify((const unsigned int)gpio_in_pins,
  976. (const unsigned int)gpio_out_pins,
  977. (const unsigned char)gpio_pa_pins,
  978. (const unsigned char)gpio_pb_pins);
  979. return 0;
  980. }
  981. void cris_io_interface_delete_watcher(void (*notify)(const unsigned int gpio_in_available,
  982. const unsigned int gpio_out_available,
  983. const unsigned char pa_available,
  984. const unsigned char pb_available))
  985. {
  986. struct watcher *w = watchers, *prev = NULL;
  987. (void)cris_io_interface_init();
  988. while ((NULL != w) && (w->notify != notify)){
  989. prev = w;
  990. w = w->next;
  991. }
  992. if (NULL != w) {
  993. if (NULL != prev) {
  994. prev->next = w->next;
  995. } else {
  996. watchers = w->next;
  997. }
  998. kfree(w);
  999. return;
  1000. }
  1001. printk(KERN_WARNING "cris_io_interface_delete_watcher: Deleting unknown watcher 0x%p\n", notify);
  1002. }
  1003. static int cris_io_interface_init(void)
  1004. {
  1005. static int first = 1;
  1006. int i;
  1007. if (!first) {
  1008. return 0;
  1009. }
  1010. first = 0;
  1011. for (i = 0; i<8; i++) {
  1012. gpio_pa_owners[i] = if_unclaimed;
  1013. gpio_pb_owners[i] = if_unclaimed;
  1014. gpio_pg_owners[i] = if_unclaimed;
  1015. }
  1016. for (; i<32; i++) {
  1017. gpio_pg_owners[i] = if_unclaimed;
  1018. }
  1019. return 0;
  1020. }
  1021. module_init(cris_io_interface_init);
  1022. EXPORT_SYMBOL(cris_request_io_interface);
  1023. EXPORT_SYMBOL(cris_free_io_interface);
  1024. EXPORT_SYMBOL(cris_io_interface_allocate_pins);
  1025. EXPORT_SYMBOL(cris_io_interface_free_pins);
  1026. EXPORT_SYMBOL(cris_io_interface_register_watcher);
  1027. EXPORT_SYMBOL(cris_io_interface_delete_watcher);