io_interface_mux.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /* IO interface mux allocator for ETRAX100LX.
  2. * Copyright 2004, Axis Communications AB
  3. * $Id: io_interface_mux.c,v 1.2 2004/12/21 12:08:38 starvik Exp $
  4. */
  5. /* C.f. ETRAX100LX Designer's Reference 20.9 */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/errno.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <asm/arch/svinto.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/io_interface_mux.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. unsigned char used;
  38. enum cris_io_interface owner;
  39. };
  40. struct interface
  41. {
  42. enum cris_io_interface ioif;
  43. unsigned char groups;
  44. unsigned char used;
  45. char *owner;
  46. unsigned int gpio_g_in;
  47. unsigned int gpio_g_out;
  48. unsigned char gpio_b;
  49. };
  50. static struct if_group if_groups[6] = {
  51. {
  52. .group = group_a,
  53. .used = 0,
  54. },
  55. {
  56. .group = group_b,
  57. .used = 0,
  58. },
  59. {
  60. .group = group_c,
  61. .used = 0,
  62. },
  63. {
  64. .group = group_d,
  65. .used = 0,
  66. },
  67. {
  68. .group = group_e,
  69. .used = 0,
  70. },
  71. {
  72. .group = group_f,
  73. .used = 0,
  74. }
  75. };
  76. /* The order in the array must match the order of enum
  77. * cris_io_interface in io_interface_mux.h */
  78. static struct interface interfaces[] = {
  79. /* Begin Non-multiplexed interfaces */
  80. {
  81. .ioif = if_eth,
  82. .groups = 0,
  83. .gpio_g_in = 0,
  84. .gpio_g_out = 0,
  85. .gpio_b = 0
  86. },
  87. {
  88. .ioif = if_serial_0,
  89. .groups = 0,
  90. .gpio_g_in = 0,
  91. .gpio_g_out = 0,
  92. .gpio_b = 0
  93. },
  94. /* End Non-multiplexed interfaces */
  95. {
  96. .ioif = if_serial_1,
  97. .groups = group_e,
  98. .gpio_g_in = 0x00000000,
  99. .gpio_g_out = 0x00000000,
  100. .gpio_b = 0x00
  101. },
  102. {
  103. .ioif = if_serial_2,
  104. .groups = group_b,
  105. .gpio_g_in = 0x000000c0,
  106. .gpio_g_out = 0x000000c0,
  107. .gpio_b = 0x00
  108. },
  109. {
  110. .ioif = if_serial_3,
  111. .groups = group_c,
  112. .gpio_g_in = 0xc0000000,
  113. .gpio_g_out = 0xc0000000,
  114. .gpio_b = 0x00
  115. },
  116. {
  117. .ioif = if_sync_serial_1,
  118. .groups = group_e | group_f, /* if_sync_serial_1 and if_sync_serial_3
  119. can be used simultaneously */
  120. .gpio_g_in = 0x00000000,
  121. .gpio_g_out = 0x00000000,
  122. .gpio_b = 0x10
  123. },
  124. {
  125. .ioif = if_sync_serial_3,
  126. .groups = group_c | group_f,
  127. .gpio_g_in = 0xc0000000,
  128. .gpio_g_out = 0xc0000000,
  129. .gpio_b = 0x80
  130. },
  131. {
  132. .ioif = if_shared_ram,
  133. .groups = group_a,
  134. .gpio_g_in = 0x0000ff3e,
  135. .gpio_g_out = 0x0000ff38,
  136. .gpio_b = 0x00
  137. },
  138. {
  139. .ioif = if_shared_ram_w,
  140. .groups = group_a | group_d,
  141. .gpio_g_in = 0x00ffff3e,
  142. .gpio_g_out = 0x00ffff38,
  143. .gpio_b = 0x00
  144. },
  145. {
  146. .ioif = if_par_0,
  147. .groups = group_a,
  148. .gpio_g_in = 0x0000ff3e,
  149. .gpio_g_out = 0x0000ff3e,
  150. .gpio_b = 0x00
  151. },
  152. {
  153. .ioif = if_par_1,
  154. .groups = group_d,
  155. .gpio_g_in = 0x3eff0000,
  156. .gpio_g_out = 0x3eff0000,
  157. .gpio_b = 0x00
  158. },
  159. {
  160. .ioif = if_par_w,
  161. .groups = group_a | group_d,
  162. .gpio_g_in = 0x00ffff3e,
  163. .gpio_g_out = 0x00ffff3e,
  164. .gpio_b = 0x00
  165. },
  166. {
  167. .ioif = if_scsi8_0,
  168. .groups = group_a | group_b | group_f, /* if_scsi8_0 and if_scsi8_1
  169. can be used simultaneously */
  170. .gpio_g_in = 0x0000ffff,
  171. .gpio_g_out = 0x0000ffff,
  172. .gpio_b = 0x10
  173. },
  174. {
  175. .ioif = if_scsi8_1,
  176. .groups = group_c | group_d | group_f, /* if_scsi8_0 and if_scsi8_1
  177. can be used simultaneously */
  178. .gpio_g_in = 0xffff0000,
  179. .gpio_g_out = 0xffff0000,
  180. .gpio_b = 0x80
  181. },
  182. {
  183. .ioif = if_scsi_w,
  184. .groups = group_a | group_b | group_d | group_f,
  185. .gpio_g_in = 0x01ffffff,
  186. .gpio_g_out = 0x07ffffff,
  187. .gpio_b = 0x80
  188. },
  189. {
  190. .ioif = if_ata,
  191. .groups = group_a | group_b | group_c | group_d,
  192. .gpio_g_in = 0xf9ffffff,
  193. .gpio_g_out = 0xffffffff,
  194. .gpio_b = 0x80
  195. },
  196. {
  197. .ioif = if_csp,
  198. .groups = group_f, /* if_csp and if_i2c can be used simultaneously */
  199. .gpio_g_in = 0x00000000,
  200. .gpio_g_out = 0x00000000,
  201. .gpio_b = 0xfc
  202. },
  203. {
  204. .ioif = if_i2c,
  205. .groups = group_f, /* if_csp and if_i2c can be used simultaneously */
  206. .gpio_g_in = 0x00000000,
  207. .gpio_g_out = 0x00000000,
  208. .gpio_b = 0x03
  209. },
  210. {
  211. .ioif = if_usb_1,
  212. .groups = group_e | group_f,
  213. .gpio_g_in = 0x00000000,
  214. .gpio_g_out = 0x00000000,
  215. .gpio_b = 0x2c
  216. },
  217. {
  218. .ioif = if_usb_2,
  219. .groups = group_d,
  220. .gpio_g_in = 0x0e000000,
  221. .gpio_g_out = 0x3c000000,
  222. .gpio_b = 0x00
  223. },
  224. /* GPIO pins */
  225. {
  226. .ioif = if_gpio_grp_a,
  227. .groups = group_a,
  228. .gpio_g_in = 0x0000ff3f,
  229. .gpio_g_out = 0x0000ff3f,
  230. .gpio_b = 0x00
  231. },
  232. {
  233. .ioif = if_gpio_grp_b,
  234. .groups = group_b,
  235. .gpio_g_in = 0x000000c0,
  236. .gpio_g_out = 0x000000c0,
  237. .gpio_b = 0x00
  238. },
  239. {
  240. .ioif = if_gpio_grp_c,
  241. .groups = group_c,
  242. .gpio_g_in = 0xc0000000,
  243. .gpio_g_out = 0xc0000000,
  244. .gpio_b = 0x00
  245. },
  246. {
  247. .ioif = if_gpio_grp_d,
  248. .groups = group_d,
  249. .gpio_g_in = 0x3fff0000,
  250. .gpio_g_out = 0x3fff0000,
  251. .gpio_b = 0x00
  252. },
  253. {
  254. .ioif = if_gpio_grp_e,
  255. .groups = group_e,
  256. .gpio_g_in = 0x00000000,
  257. .gpio_g_out = 0x00000000,
  258. .gpio_b = 0x00
  259. },
  260. {
  261. .ioif = if_gpio_grp_f,
  262. .groups = group_f,
  263. .gpio_g_in = 0x00000000,
  264. .gpio_g_out = 0x00000000,
  265. .gpio_b = 0xff
  266. }
  267. /* Array end */
  268. };
  269. static struct watcher *watchers = NULL;
  270. static unsigned int gpio_in_pins = 0xffffffff;
  271. static unsigned int gpio_out_pins = 0xffffffff;
  272. static unsigned char gpio_pb_pins = 0xff;
  273. static unsigned char gpio_pa_pins = 0xff;
  274. static enum cris_io_interface gpio_pa_owners[8];
  275. static enum cris_io_interface gpio_pb_owners[8];
  276. static enum cris_io_interface gpio_pg_owners[32];
  277. static int cris_io_interface_init(void);
  278. static unsigned char clear_group_from_set(const unsigned char groups, struct if_group *group)
  279. {
  280. return (groups & ~group->group);
  281. }
  282. static struct if_group *get_group(const unsigned char groups)
  283. {
  284. int i;
  285. for (i = 0; i < sizeof(if_groups)/sizeof(struct if_group); i++) {
  286. if (groups & if_groups[i].group) {
  287. return &if_groups[i];
  288. }
  289. }
  290. return NULL;
  291. }
  292. static void notify_watchers(void)
  293. {
  294. struct watcher *w = watchers;
  295. DBG(printk("io_interface_mux: notifying watchers\n"));
  296. while (NULL != w) {
  297. w->notify((const unsigned int)gpio_in_pins,
  298. (const unsigned int)gpio_out_pins,
  299. (const unsigned char)gpio_pa_pins,
  300. (const unsigned char)gpio_pb_pins);
  301. w = w->next;
  302. }
  303. }
  304. int cris_request_io_interface(enum cris_io_interface ioif, const char *device_id)
  305. {
  306. int set_gen_config = 0;
  307. int set_gen_config_ii = 0;
  308. unsigned long int gens;
  309. unsigned long int gens_ii;
  310. struct if_group *grp;
  311. unsigned char group_set;
  312. unsigned long flags;
  313. (void)cris_io_interface_init();
  314. DBG(printk("cris_request_io_interface(%d, \"%s\")\n", ioif, device_id));
  315. if ((ioif >= if_max_interfaces) || (ioif < 0)) {
  316. printk(KERN_CRIT "cris_request_io_interface: Bad interface %u submitted for %s\n",
  317. ioif,
  318. device_id);
  319. return -EINVAL;
  320. }
  321. local_irq_save(flags);
  322. if (interfaces[ioif].used) {
  323. local_irq_restore(flags);
  324. printk(KERN_CRIT "cris_io_interface: Cannot allocate interface for %s, in use by %s\n",
  325. device_id,
  326. interfaces[ioif].owner);
  327. return -EBUSY;
  328. }
  329. /* Check that all required groups are free before allocating, */
  330. group_set = interfaces[ioif].groups;
  331. while (NULL != (grp = get_group(group_set))) {
  332. if (grp->used) {
  333. if (grp->group == group_f) {
  334. if ((if_sync_serial_1 == ioif) ||
  335. (if_sync_serial_3 == ioif)) {
  336. if ((grp->owner != if_sync_serial_1) &&
  337. (grp->owner != if_sync_serial_3)) {
  338. local_irq_restore(flags);
  339. return -EBUSY;
  340. }
  341. } else if ((if_scsi8_0 == ioif) ||
  342. (if_scsi8_1 == ioif)) {
  343. if ((grp->owner != if_scsi8_0) &&
  344. (grp->owner != if_scsi8_1)) {
  345. local_irq_restore(flags);
  346. return -EBUSY;
  347. }
  348. }
  349. } else {
  350. local_irq_restore(flags);
  351. return -EBUSY;
  352. }
  353. }
  354. group_set = clear_group_from_set(group_set, grp);
  355. }
  356. /* Are the required GPIO pins available too? */
  357. if (((interfaces[ioif].gpio_g_in & gpio_in_pins) != interfaces[ioif].gpio_g_in) ||
  358. ((interfaces[ioif].gpio_g_out & gpio_out_pins) != interfaces[ioif].gpio_g_out) ||
  359. ((interfaces[ioif].gpio_b & gpio_pb_pins) != interfaces[ioif].gpio_b)) {
  360. printk(KERN_CRIT "cris_request_io_interface: Could not get required pins for interface %u\n",
  361. ioif);
  362. return -EBUSY;
  363. }
  364. /* All needed I/O pins and pin groups are free, allocate. */
  365. group_set = interfaces[ioif].groups;
  366. while (NULL != (grp = get_group(group_set))) {
  367. grp->used = 1;
  368. grp->owner = ioif;
  369. group_set = clear_group_from_set(group_set, grp);
  370. }
  371. gens = genconfig_shadow;
  372. gens_ii = gen_config_ii_shadow;
  373. set_gen_config = 1;
  374. switch (ioif)
  375. {
  376. /* Begin Non-multiplexed interfaces */
  377. case if_eth:
  378. /* fall through */
  379. case if_serial_0:
  380. set_gen_config = 0;
  381. break;
  382. /* End Non-multiplexed interfaces */
  383. case if_serial_1:
  384. set_gen_config_ii = 1;
  385. SETS(gens_ii, R_GEN_CONFIG_II, sermode1, async);
  386. break;
  387. case if_serial_2:
  388. SETS(gens, R_GEN_CONFIG, ser2, select);
  389. break;
  390. case if_serial_3:
  391. SETS(gens, R_GEN_CONFIG, ser3, select);
  392. set_gen_config_ii = 1;
  393. SETS(gens_ii, R_GEN_CONFIG_II, sermode3, async);
  394. break;
  395. case if_sync_serial_1:
  396. set_gen_config_ii = 1;
  397. SETS(gens_ii, R_GEN_CONFIG_II, sermode1, sync);
  398. break;
  399. case if_sync_serial_3:
  400. SETS(gens, R_GEN_CONFIG, ser3, select);
  401. set_gen_config_ii = 1;
  402. SETS(gens_ii, R_GEN_CONFIG_II, sermode3, sync);
  403. break;
  404. case if_shared_ram:
  405. SETS(gens, R_GEN_CONFIG, mio, select);
  406. break;
  407. case if_shared_ram_w:
  408. SETS(gens, R_GEN_CONFIG, mio_w, select);
  409. break;
  410. case if_par_0:
  411. SETS(gens, R_GEN_CONFIG, par0, select);
  412. break;
  413. case if_par_1:
  414. SETS(gens, R_GEN_CONFIG, par1, select);
  415. break;
  416. case if_par_w:
  417. SETS(gens, R_GEN_CONFIG, par0, select);
  418. SETS(gens, R_GEN_CONFIG, par_w, select);
  419. break;
  420. case if_scsi8_0:
  421. SETS(gens, R_GEN_CONFIG, scsi0, select);
  422. break;
  423. case if_scsi8_1:
  424. SETS(gens, R_GEN_CONFIG, scsi1, select);
  425. break;
  426. case if_scsi_w:
  427. SETS(gens, R_GEN_CONFIG, scsi0, select);
  428. SETS(gens, R_GEN_CONFIG, scsi0w, select);
  429. break;
  430. case if_ata:
  431. SETS(gens, R_GEN_CONFIG, ata, select);
  432. break;
  433. case if_csp:
  434. /* fall through */
  435. case if_i2c:
  436. set_gen_config = 0;
  437. break;
  438. case if_usb_1:
  439. SETS(gens, R_GEN_CONFIG, usb1, select);
  440. break;
  441. case if_usb_2:
  442. SETS(gens, R_GEN_CONFIG, usb2, select);
  443. break;
  444. case if_gpio_grp_a:
  445. /* GPIO groups are only accounted, don't do configuration changes. */
  446. /* fall through */
  447. case if_gpio_grp_b:
  448. /* fall through */
  449. case if_gpio_grp_c:
  450. /* fall through */
  451. case if_gpio_grp_d:
  452. /* fall through */
  453. case if_gpio_grp_e:
  454. /* fall through */
  455. case if_gpio_grp_f:
  456. set_gen_config = 0;
  457. break;
  458. default:
  459. panic("cris_request_io_interface: Bad interface %u submitted for %s\n",
  460. ioif,
  461. device_id);
  462. }
  463. interfaces[ioif].used = 1;
  464. interfaces[ioif].owner = (char*)device_id;
  465. if (set_gen_config) {
  466. volatile int i;
  467. genconfig_shadow = gens;
  468. *R_GEN_CONFIG = genconfig_shadow;
  469. /* Wait 12 cycles before doing any DMA command */
  470. for(i = 6; i > 0; i--)
  471. nop();
  472. }
  473. if (set_gen_config_ii) {
  474. gen_config_ii_shadow = gens_ii;
  475. *R_GEN_CONFIG_II = gen_config_ii_shadow;
  476. }
  477. DBG(printk("GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  478. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  479. DBG(printk("grabbing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  480. interfaces[ioif].gpio_g_in,
  481. interfaces[ioif].gpio_g_out,
  482. interfaces[ioif].gpio_b));
  483. gpio_in_pins &= ~interfaces[ioif].gpio_g_in;
  484. gpio_out_pins &= ~interfaces[ioif].gpio_g_out;
  485. gpio_pb_pins &= ~interfaces[ioif].gpio_b;
  486. DBG(printk("GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  487. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  488. local_irq_restore(flags);
  489. notify_watchers();
  490. return 0;
  491. }
  492. void cris_free_io_interface(enum cris_io_interface ioif)
  493. {
  494. struct if_group *grp;
  495. unsigned char group_set;
  496. unsigned long flags;
  497. (void)cris_io_interface_init();
  498. if ((ioif >= if_max_interfaces) || (ioif < 0)) {
  499. printk(KERN_CRIT "cris_free_io_interface: Bad interface %u\n",
  500. ioif);
  501. return;
  502. }
  503. local_irq_save(flags);
  504. if (!interfaces[ioif].used) {
  505. printk(KERN_CRIT "cris_free_io_interface: Freeing free interface %u\n",
  506. ioif);
  507. local_irq_restore(flags);
  508. return;
  509. }
  510. group_set = interfaces[ioif].groups;
  511. while (NULL != (grp = get_group(group_set))) {
  512. if (grp->group == group_f) {
  513. switch (ioif)
  514. {
  515. case if_sync_serial_1:
  516. if ((grp->owner == if_sync_serial_1) &&
  517. interfaces[if_sync_serial_3].used) {
  518. grp->owner = if_sync_serial_3;
  519. } else
  520. grp->used = 0;
  521. break;
  522. case if_sync_serial_3:
  523. if ((grp->owner == if_sync_serial_3) &&
  524. interfaces[if_sync_serial_1].used) {
  525. grp->owner = if_sync_serial_1;
  526. } else
  527. grp->used = 0;
  528. break;
  529. case if_scsi8_0:
  530. if ((grp->owner == if_scsi8_0) &&
  531. interfaces[if_scsi8_1].used) {
  532. grp->owner = if_scsi8_1;
  533. } else
  534. grp->used = 0;
  535. break;
  536. case if_scsi8_1:
  537. if ((grp->owner == if_scsi8_1) &&
  538. interfaces[if_scsi8_0].used) {
  539. grp->owner = if_scsi8_0;
  540. } else
  541. grp->used = 0;
  542. break;
  543. default:
  544. grp->used = 0;
  545. }
  546. } else {
  547. grp->used = 0;
  548. }
  549. group_set = clear_group_from_set(group_set, grp);
  550. }
  551. interfaces[ioif].used = 0;
  552. interfaces[ioif].owner = NULL;
  553. DBG(printk("GPIO pins: available before: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  554. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  555. DBG(printk("freeing pins: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  556. interfaces[ioif].gpio_g_in,
  557. interfaces[ioif].gpio_g_out,
  558. interfaces[ioif].gpio_b));
  559. gpio_in_pins |= interfaces[ioif].gpio_g_in;
  560. gpio_out_pins |= interfaces[ioif].gpio_g_out;
  561. gpio_pb_pins |= interfaces[ioif].gpio_b;
  562. DBG(printk("GPIO pins: available after: g_in=0x%08x g_out=0x%08x pb=0x%02x\n",
  563. gpio_in_pins, gpio_out_pins, gpio_pb_pins));
  564. local_irq_restore(flags);
  565. notify_watchers();
  566. }
  567. /* Create a bitmask from bit 0 (inclusive) to bit stop_bit
  568. (non-inclusive). stop_bit == 0 returns 0x0 */
  569. static inline unsigned int create_mask(const unsigned stop_bit)
  570. {
  571. /* Avoid overflow */
  572. if (stop_bit >= 32) {
  573. return 0xffffffff;
  574. }
  575. return (1<<stop_bit)-1;
  576. }
  577. /* port can be 'a', 'b' or 'g' */
  578. int cris_io_interface_allocate_pins(const enum cris_io_interface ioif,
  579. const char port,
  580. const unsigned start_bit,
  581. const unsigned stop_bit)
  582. {
  583. unsigned int i;
  584. unsigned int mask = 0;
  585. unsigned int tmp_mask;
  586. unsigned long int flags;
  587. enum cris_io_interface *owners;
  588. (void)cris_io_interface_init();
  589. DBG(printk("cris_io_interface_allocate_pins: if=%d port=%c start=%u stop=%u\n",
  590. ioif, port, start_bit, stop_bit));
  591. if (!((start_bit <= stop_bit) &&
  592. ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
  593. ((port == 'g') && (stop_bit < 32))))) {
  594. return -EINVAL;
  595. }
  596. mask = create_mask(stop_bit + 1);
  597. tmp_mask = create_mask(start_bit);
  598. mask &= ~tmp_mask;
  599. DBG(printk("cris_io_interface_allocate_pins: port=%c start=%u stop=%u mask=0x%08x\n",
  600. port, start_bit, stop_bit, mask));
  601. local_irq_save(flags);
  602. switch (port) {
  603. case 'a':
  604. if ((gpio_pa_pins & mask) != mask) {
  605. local_irq_restore(flags);
  606. return -EBUSY;
  607. }
  608. owners = gpio_pa_owners;
  609. gpio_pa_pins &= ~mask;
  610. break;
  611. case 'b':
  612. if ((gpio_pb_pins & mask) != mask) {
  613. local_irq_restore(flags);
  614. return -EBUSY;
  615. }
  616. owners = gpio_pb_owners;
  617. gpio_pb_pins &= ~mask;
  618. break;
  619. case 'g':
  620. if (((gpio_in_pins & mask) != mask) ||
  621. ((gpio_out_pins & mask) != mask)) {
  622. local_irq_restore(flags);
  623. return -EBUSY;
  624. }
  625. owners = gpio_pg_owners;
  626. gpio_in_pins &= ~mask;
  627. gpio_out_pins &= ~mask;
  628. break;
  629. default:
  630. local_irq_restore(flags);
  631. return -EINVAL;
  632. }
  633. for (i = start_bit; i <= stop_bit; i++) {
  634. owners[i] = ioif;
  635. }
  636. local_irq_restore(flags);
  637. notify_watchers();
  638. return 0;
  639. }
  640. /* port can be 'a', 'b' or 'g' */
  641. int cris_io_interface_free_pins(const enum cris_io_interface ioif,
  642. const char port,
  643. const unsigned start_bit,
  644. const unsigned stop_bit)
  645. {
  646. unsigned int i;
  647. unsigned int mask = 0;
  648. unsigned int tmp_mask;
  649. unsigned long int flags;
  650. enum cris_io_interface *owners;
  651. (void)cris_io_interface_init();
  652. if (!((start_bit <= stop_bit) &&
  653. ((((port == 'a') || (port == 'b')) && (stop_bit < 8)) ||
  654. ((port == 'g') && (stop_bit < 32))))) {
  655. return -EINVAL;
  656. }
  657. mask = create_mask(stop_bit + 1);
  658. tmp_mask = create_mask(start_bit);
  659. mask &= ~tmp_mask;
  660. DBG(printk("cris_io_interface_free_pins: port=%c start=%u stop=%u mask=0x%08x\n",
  661. port, start_bit, stop_bit, mask));
  662. local_irq_save(flags);
  663. switch (port) {
  664. case 'a':
  665. if ((~gpio_pa_pins & mask) != mask) {
  666. local_irq_restore(flags);
  667. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  668. }
  669. owners = gpio_pa_owners;
  670. break;
  671. case 'b':
  672. if ((~gpio_pb_pins & mask) != mask) {
  673. local_irq_restore(flags);
  674. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  675. }
  676. owners = gpio_pb_owners;
  677. break;
  678. case 'g':
  679. if (((~gpio_in_pins & mask) != mask) ||
  680. ((~gpio_out_pins & mask) != mask)) {
  681. local_irq_restore(flags);
  682. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing free pins");
  683. }
  684. owners = gpio_pg_owners;
  685. break;
  686. default:
  687. owners = NULL; /* Cannot happen. Shut up, gcc! */
  688. }
  689. for (i = start_bit; i <= stop_bit; i++) {
  690. if (owners[i] != ioif) {
  691. printk(KERN_CRIT "cris_io_interface_free_pins: Freeing unowned pins");
  692. }
  693. }
  694. /* All was ok, change data. */
  695. switch (port) {
  696. case 'a':
  697. gpio_pa_pins |= mask;
  698. break;
  699. case 'b':
  700. gpio_pb_pins |= mask;
  701. break;
  702. case 'g':
  703. gpio_in_pins |= mask;
  704. gpio_out_pins |= mask;
  705. break;
  706. }
  707. for (i = start_bit; i <= stop_bit; i++) {
  708. owners[i] = if_unclaimed;
  709. }
  710. local_irq_restore(flags);
  711. notify_watchers();
  712. return 0;
  713. }
  714. int cris_io_interface_register_watcher(void (*notify)(const unsigned int gpio_in_available,
  715. const unsigned int gpio_out_available,
  716. const unsigned char pa_available,
  717. const unsigned char pb_available))
  718. {
  719. struct watcher *w;
  720. (void)cris_io_interface_init();
  721. if (NULL == notify) {
  722. return -EINVAL;
  723. }
  724. w = kmalloc(sizeof(*w), GFP_KERNEL);
  725. if (!w) {
  726. return -ENOMEM;
  727. }
  728. w->notify = notify;
  729. w->next = watchers;
  730. watchers = w;
  731. w->notify((const unsigned int)gpio_in_pins,
  732. (const unsigned int)gpio_out_pins,
  733. (const unsigned char)gpio_pa_pins,
  734. (const unsigned char)gpio_pb_pins);
  735. return 0;
  736. }
  737. void cris_io_interface_delete_watcher(void (*notify)(const unsigned int gpio_in_available,
  738. const unsigned int gpio_out_available,
  739. const unsigned char pa_available,
  740. const unsigned char pb_available))
  741. {
  742. struct watcher *w = watchers, *prev = NULL;
  743. (void)cris_io_interface_init();
  744. while ((NULL != w) && (w->notify != notify)){
  745. prev = w;
  746. w = w->next;
  747. }
  748. if (NULL != w) {
  749. if (NULL != prev) {
  750. prev->next = w->next;
  751. } else {
  752. watchers = w->next;
  753. }
  754. kfree(w);
  755. return;
  756. }
  757. printk(KERN_WARNING "cris_io_interface_delete_watcher: Deleting unknown watcher 0x%p\n", notify);
  758. }
  759. static int cris_io_interface_init(void)
  760. {
  761. static int first = 1;
  762. int i;
  763. if (!first) {
  764. return 0;
  765. }
  766. first = 0;
  767. for (i = 0; i<8; i++) {
  768. gpio_pa_owners[i] = if_unclaimed;
  769. gpio_pb_owners[i] = if_unclaimed;
  770. gpio_pg_owners[i] = if_unclaimed;
  771. }
  772. for (; i<32; i++) {
  773. gpio_pg_owners[i] = if_unclaimed;
  774. }
  775. return 0;
  776. }
  777. module_init(cris_io_interface_init);
  778. EXPORT_SYMBOL(cris_request_io_interface);
  779. EXPORT_SYMBOL(cris_free_io_interface);
  780. EXPORT_SYMBOL(cris_io_interface_allocate_pins);
  781. EXPORT_SYMBOL(cris_io_interface_free_pins);
  782. EXPORT_SYMBOL(cris_io_interface_register_watcher);
  783. EXPORT_SYMBOL(cris_io_interface_delete_watcher);