bfin_gpio.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * GPIO Abstraction Layer
  3. *
  4. * Copyright 2006-2010 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later
  7. */
  8. #include <linux/delay.h>
  9. #include <linux/module.h>
  10. #include <linux/err.h>
  11. #include <linux/proc_fs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/gpio/driver.h>
  14. /* FIXME: consumer API required for gpio_set_value() etc, get rid of this */
  15. #include <linux/gpio.h>
  16. #include <linux/irq.h>
  17. #include <asm/gpio.h>
  18. #include <asm/irq_handler.h>
  19. #include <asm/portmux.h>
  20. #if ANOMALY_05000311 || ANOMALY_05000323
  21. enum {
  22. AWA_data = SYSCR,
  23. AWA_data_clear = SYSCR,
  24. AWA_data_set = SYSCR,
  25. AWA_toggle = SYSCR,
  26. AWA_maska = BFIN_UART_SCR,
  27. AWA_maska_clear = BFIN_UART_SCR,
  28. AWA_maska_set = BFIN_UART_SCR,
  29. AWA_maska_toggle = BFIN_UART_SCR,
  30. AWA_maskb = BFIN_UART_GCTL,
  31. AWA_maskb_clear = BFIN_UART_GCTL,
  32. AWA_maskb_set = BFIN_UART_GCTL,
  33. AWA_maskb_toggle = BFIN_UART_GCTL,
  34. AWA_dir = SPORT1_STAT,
  35. AWA_polar = SPORT1_STAT,
  36. AWA_edge = SPORT1_STAT,
  37. AWA_both = SPORT1_STAT,
  38. #if ANOMALY_05000311
  39. AWA_inen = TIMER_ENABLE,
  40. #elif ANOMALY_05000323
  41. AWA_inen = DMA1_1_CONFIG,
  42. #endif
  43. };
  44. /* Anomaly Workaround */
  45. #define AWA_DUMMY_READ(name) bfin_read16(AWA_ ## name)
  46. #else
  47. #define AWA_DUMMY_READ(...) do { } while (0)
  48. #endif
  49. static struct gpio_port_t * const gpio_array[] = {
  50. #if defined(BF533_FAMILY) || defined(BF538_FAMILY)
  51. (struct gpio_port_t *) FIO_FLAG_D,
  52. #elif defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  53. (struct gpio_port_t *) PORTFIO,
  54. (struct gpio_port_t *) PORTGIO,
  55. (struct gpio_port_t *) PORTHIO,
  56. #elif defined(BF561_FAMILY)
  57. (struct gpio_port_t *) FIO0_FLAG_D,
  58. (struct gpio_port_t *) FIO1_FLAG_D,
  59. (struct gpio_port_t *) FIO2_FLAG_D,
  60. #else
  61. # error no gpio arrays defined
  62. #endif
  63. };
  64. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  65. static unsigned short * const port_fer[] = {
  66. (unsigned short *) PORTF_FER,
  67. (unsigned short *) PORTG_FER,
  68. (unsigned short *) PORTH_FER,
  69. };
  70. # if !defined(BF537_FAMILY)
  71. static unsigned short * const port_mux[] = {
  72. (unsigned short *) PORTF_MUX,
  73. (unsigned short *) PORTG_MUX,
  74. (unsigned short *) PORTH_MUX,
  75. };
  76. static const
  77. u8 pmux_offset[][16] = {
  78. # if defined(CONFIG_BF52x)
  79. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 4, 6, 8, 8, 10, 10 }, /* PORTF */
  80. { 0, 0, 0, 0, 0, 2, 2, 4, 4, 6, 8, 10, 10, 10, 12, 12 }, /* PORTG */
  81. { 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 4, 4, 4, 4, 4, 4 }, /* PORTH */
  82. # elif defined(CONFIG_BF51x)
  83. { 0, 2, 2, 2, 2, 2, 2, 4, 6, 6, 6, 8, 8, 8, 8, 10 }, /* PORTF */
  84. { 0, 0, 0, 2, 4, 6, 6, 6, 8, 10, 10, 12, 14, 14, 14, 14 }, /* PORTG */
  85. { 0, 0, 0, 0, 2, 2, 4, 6, 10, 10, 10, 10, 10, 10, 10, 10 }, /* PORTH */
  86. # endif
  87. };
  88. # endif
  89. #elif defined(BF538_FAMILY)
  90. static unsigned short * const port_fer[] = {
  91. (unsigned short *) PORTCIO_FER,
  92. (unsigned short *) PORTDIO_FER,
  93. (unsigned short *) PORTEIO_FER,
  94. };
  95. #endif
  96. #define RESOURCE_LABEL_SIZE 16
  97. static struct str_ident {
  98. char name[RESOURCE_LABEL_SIZE];
  99. } str_ident[MAX_RESOURCES];
  100. #if defined(CONFIG_PM)
  101. static struct gpio_port_s gpio_bank_saved[GPIO_BANK_NUM];
  102. # ifdef BF538_FAMILY
  103. static unsigned short port_fer_saved[3];
  104. # endif
  105. #endif
  106. static void gpio_error(unsigned gpio)
  107. {
  108. printk(KERN_ERR "bfin-gpio: GPIO %d wasn't requested!\n", gpio);
  109. }
  110. static void set_label(unsigned short ident, const char *label)
  111. {
  112. if (label) {
  113. strncpy(str_ident[ident].name, label,
  114. RESOURCE_LABEL_SIZE);
  115. str_ident[ident].name[RESOURCE_LABEL_SIZE - 1] = 0;
  116. }
  117. }
  118. static char *get_label(unsigned short ident)
  119. {
  120. return (*str_ident[ident].name ? str_ident[ident].name : "UNKNOWN");
  121. }
  122. static int cmp_label(unsigned short ident, const char *label)
  123. {
  124. if (label == NULL) {
  125. dump_stack();
  126. printk(KERN_ERR "Please provide none-null label\n");
  127. }
  128. if (label)
  129. return strcmp(str_ident[ident].name, label);
  130. else
  131. return -EINVAL;
  132. }
  133. #define map_entry(m, i) reserved_##m##_map[gpio_bank(i)]
  134. #define is_reserved(m, i, e) (map_entry(m, i) & gpio_bit(i))
  135. #define reserve(m, i) (map_entry(m, i) |= gpio_bit(i))
  136. #define unreserve(m, i) (map_entry(m, i) &= ~gpio_bit(i))
  137. #define DECLARE_RESERVED_MAP(m, c) static unsigned short reserved_##m##_map[c]
  138. DECLARE_RESERVED_MAP(gpio, GPIO_BANK_NUM);
  139. DECLARE_RESERVED_MAP(peri, DIV_ROUND_UP(MAX_RESOURCES, GPIO_BANKSIZE));
  140. DECLARE_RESERVED_MAP(gpio_irq, GPIO_BANK_NUM);
  141. inline int check_gpio(unsigned gpio)
  142. {
  143. if (gpio >= MAX_BLACKFIN_GPIOS)
  144. return -EINVAL;
  145. return 0;
  146. }
  147. static void port_setup(unsigned gpio, unsigned short usage)
  148. {
  149. #if defined(BF538_FAMILY)
  150. /*
  151. * BF538/9 Port C,D and E are special.
  152. * Inverted PORT_FER polarity on CDE and no PORF_FER on F
  153. * Regular PORT F GPIOs are handled here, CDE are exclusively
  154. * managed by GPIOLIB
  155. */
  156. if (gpio < MAX_BLACKFIN_GPIOS || gpio >= MAX_RESOURCES)
  157. return;
  158. gpio -= MAX_BLACKFIN_GPIOS;
  159. if (usage == GPIO_USAGE)
  160. *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
  161. else
  162. *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  163. SSYNC();
  164. return;
  165. #endif
  166. if (check_gpio(gpio))
  167. return;
  168. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  169. if (usage == GPIO_USAGE)
  170. *port_fer[gpio_bank(gpio)] &= ~gpio_bit(gpio);
  171. else
  172. *port_fer[gpio_bank(gpio)] |= gpio_bit(gpio);
  173. SSYNC();
  174. #endif
  175. }
  176. #ifdef BF537_FAMILY
  177. static const s8 port_mux[] = {
  178. [GPIO_PF0] = 3,
  179. [GPIO_PF1] = 3,
  180. [GPIO_PF2] = 4,
  181. [GPIO_PF3] = 4,
  182. [GPIO_PF4] = 5,
  183. [GPIO_PF5] = 6,
  184. [GPIO_PF6] = 7,
  185. [GPIO_PF7] = 8,
  186. [GPIO_PF8 ... GPIO_PF15] = -1,
  187. [GPIO_PG0 ... GPIO_PG7] = -1,
  188. [GPIO_PG8] = 9,
  189. [GPIO_PG9] = 9,
  190. [GPIO_PG10] = 10,
  191. [GPIO_PG11] = 10,
  192. [GPIO_PG12] = 10,
  193. [GPIO_PG13] = 11,
  194. [GPIO_PG14] = 11,
  195. [GPIO_PG15] = 11,
  196. [GPIO_PH0 ... GPIO_PH15] = -1,
  197. [PORT_PJ0 ... PORT_PJ3] = -1,
  198. [PORT_PJ4] = 1,
  199. [PORT_PJ5] = 1,
  200. [PORT_PJ6 ... PORT_PJ9] = -1,
  201. [PORT_PJ10] = 0,
  202. [PORT_PJ11] = 0,
  203. };
  204. static int portmux_group_check(unsigned short per)
  205. {
  206. u16 ident = P_IDENT(per);
  207. u16 function = P_FUNCT2MUX(per);
  208. s8 offset = port_mux[ident];
  209. u16 m, pmux, pfunc, mask;
  210. if (offset < 0)
  211. return 0;
  212. pmux = bfin_read_PORT_MUX();
  213. for (m = 0; m < ARRAY_SIZE(port_mux); ++m) {
  214. if (m == ident)
  215. continue;
  216. if (port_mux[m] != offset)
  217. continue;
  218. if (!is_reserved(peri, m, 1))
  219. continue;
  220. if (offset == 1)
  221. mask = 3;
  222. else
  223. mask = 1;
  224. pfunc = (pmux >> offset) & mask;
  225. if (pfunc != (function & mask)) {
  226. pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
  227. ident, function, m, pfunc);
  228. return -EINVAL;
  229. }
  230. }
  231. return 0;
  232. }
  233. static void portmux_setup(unsigned short per)
  234. {
  235. u16 ident = P_IDENT(per);
  236. u16 function = P_FUNCT2MUX(per);
  237. s8 offset = port_mux[ident];
  238. u16 pmux, mask;
  239. if (offset == -1)
  240. return;
  241. pmux = bfin_read_PORT_MUX();
  242. if (offset == 1)
  243. mask = 3;
  244. else
  245. mask = 1;
  246. pmux &= ~(mask << offset);
  247. pmux |= ((function & mask) << offset);
  248. bfin_write_PORT_MUX(pmux);
  249. }
  250. #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  251. static int portmux_group_check(unsigned short per)
  252. {
  253. u16 ident = P_IDENT(per);
  254. u16 function = P_FUNCT2MUX(per);
  255. u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
  256. u16 pin, gpiopin, pfunc;
  257. for (pin = 0; pin < GPIO_BANKSIZE; ++pin) {
  258. if (offset != pmux_offset[gpio_bank(ident)][pin])
  259. continue;
  260. gpiopin = gpio_bank(ident) * GPIO_BANKSIZE + pin;
  261. if (gpiopin == ident)
  262. continue;
  263. if (!is_reserved(peri, gpiopin, 1))
  264. continue;
  265. pfunc = *port_mux[gpio_bank(ident)];
  266. pfunc = (pfunc >> offset) & 3;
  267. if (pfunc != function) {
  268. pr_err("pin group conflict! request pin %d func %d conflict with pin %d func %d\n",
  269. ident, function, gpiopin, pfunc);
  270. return -EINVAL;
  271. }
  272. }
  273. return 0;
  274. }
  275. inline void portmux_setup(unsigned short per)
  276. {
  277. u16 ident = P_IDENT(per);
  278. u16 function = P_FUNCT2MUX(per);
  279. u8 offset = pmux_offset[gpio_bank(ident)][gpio_sub_n(ident)];
  280. u16 pmux;
  281. pmux = *port_mux[gpio_bank(ident)];
  282. if (((pmux >> offset) & 3) == function)
  283. return;
  284. pmux &= ~(3 << offset);
  285. pmux |= (function & 3) << offset;
  286. *port_mux[gpio_bank(ident)] = pmux;
  287. SSYNC();
  288. }
  289. #else
  290. # define portmux_setup(...) do { } while (0)
  291. static int portmux_group_check(unsigned short per)
  292. {
  293. return 0;
  294. }
  295. #endif
  296. /***********************************************************
  297. *
  298. * FUNCTIONS: Blackfin General Purpose Ports Access Functions
  299. *
  300. * INPUTS/OUTPUTS:
  301. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  302. *
  303. *
  304. * DESCRIPTION: These functions abstract direct register access
  305. * to Blackfin processor General Purpose
  306. * Ports Regsiters
  307. *
  308. * CAUTION: These functions do not belong to the GPIO Driver API
  309. *************************************************************
  310. * MODIFICATION HISTORY :
  311. **************************************************************/
  312. /* Set a specific bit */
  313. #define SET_GPIO(name) \
  314. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  315. { \
  316. unsigned long flags; \
  317. flags = hard_local_irq_save(); \
  318. if (arg) \
  319. gpio_array[gpio_bank(gpio)]->name |= gpio_bit(gpio); \
  320. else \
  321. gpio_array[gpio_bank(gpio)]->name &= ~gpio_bit(gpio); \
  322. AWA_DUMMY_READ(name); \
  323. hard_local_irq_restore(flags); \
  324. } \
  325. EXPORT_SYMBOL(set_gpio_ ## name);
  326. SET_GPIO(dir) /* set_gpio_dir() */
  327. SET_GPIO(inen) /* set_gpio_inen() */
  328. SET_GPIO(polar) /* set_gpio_polar() */
  329. SET_GPIO(edge) /* set_gpio_edge() */
  330. SET_GPIO(both) /* set_gpio_both() */
  331. #define SET_GPIO_SC(name) \
  332. void set_gpio_ ## name(unsigned gpio, unsigned short arg) \
  333. { \
  334. unsigned long flags; \
  335. if (ANOMALY_05000311 || ANOMALY_05000323) \
  336. flags = hard_local_irq_save(); \
  337. if (arg) \
  338. gpio_array[gpio_bank(gpio)]->name ## _set = gpio_bit(gpio); \
  339. else \
  340. gpio_array[gpio_bank(gpio)]->name ## _clear = gpio_bit(gpio); \
  341. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  342. AWA_DUMMY_READ(name); \
  343. hard_local_irq_restore(flags); \
  344. } \
  345. } \
  346. EXPORT_SYMBOL(set_gpio_ ## name);
  347. SET_GPIO_SC(maska)
  348. SET_GPIO_SC(maskb)
  349. SET_GPIO_SC(data)
  350. void set_gpio_toggle(unsigned gpio)
  351. {
  352. unsigned long flags;
  353. if (ANOMALY_05000311 || ANOMALY_05000323)
  354. flags = hard_local_irq_save();
  355. gpio_array[gpio_bank(gpio)]->toggle = gpio_bit(gpio);
  356. if (ANOMALY_05000311 || ANOMALY_05000323) {
  357. AWA_DUMMY_READ(toggle);
  358. hard_local_irq_restore(flags);
  359. }
  360. }
  361. EXPORT_SYMBOL(set_gpio_toggle);
  362. /*Set current PORT date (16-bit word)*/
  363. #define SET_GPIO_P(name) \
  364. void set_gpiop_ ## name(unsigned gpio, unsigned short arg) \
  365. { \
  366. unsigned long flags; \
  367. if (ANOMALY_05000311 || ANOMALY_05000323) \
  368. flags = hard_local_irq_save(); \
  369. gpio_array[gpio_bank(gpio)]->name = arg; \
  370. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  371. AWA_DUMMY_READ(name); \
  372. hard_local_irq_restore(flags); \
  373. } \
  374. } \
  375. EXPORT_SYMBOL(set_gpiop_ ## name);
  376. SET_GPIO_P(data)
  377. SET_GPIO_P(dir)
  378. SET_GPIO_P(inen)
  379. SET_GPIO_P(polar)
  380. SET_GPIO_P(edge)
  381. SET_GPIO_P(both)
  382. SET_GPIO_P(maska)
  383. SET_GPIO_P(maskb)
  384. /* Get a specific bit */
  385. #define GET_GPIO(name) \
  386. unsigned short get_gpio_ ## name(unsigned gpio) \
  387. { \
  388. unsigned long flags; \
  389. unsigned short ret; \
  390. if (ANOMALY_05000311 || ANOMALY_05000323) \
  391. flags = hard_local_irq_save(); \
  392. ret = 0x01 & (gpio_array[gpio_bank(gpio)]->name >> gpio_sub_n(gpio)); \
  393. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  394. AWA_DUMMY_READ(name); \
  395. hard_local_irq_restore(flags); \
  396. } \
  397. return ret; \
  398. } \
  399. EXPORT_SYMBOL(get_gpio_ ## name);
  400. GET_GPIO(data)
  401. GET_GPIO(dir)
  402. GET_GPIO(inen)
  403. GET_GPIO(polar)
  404. GET_GPIO(edge)
  405. GET_GPIO(both)
  406. GET_GPIO(maska)
  407. GET_GPIO(maskb)
  408. /*Get current PORT date (16-bit word)*/
  409. #define GET_GPIO_P(name) \
  410. unsigned short get_gpiop_ ## name(unsigned gpio) \
  411. { \
  412. unsigned long flags; \
  413. unsigned short ret; \
  414. if (ANOMALY_05000311 || ANOMALY_05000323) \
  415. flags = hard_local_irq_save(); \
  416. ret = (gpio_array[gpio_bank(gpio)]->name); \
  417. if (ANOMALY_05000311 || ANOMALY_05000323) { \
  418. AWA_DUMMY_READ(name); \
  419. hard_local_irq_restore(flags); \
  420. } \
  421. return ret; \
  422. } \
  423. EXPORT_SYMBOL(get_gpiop_ ## name);
  424. GET_GPIO_P(data)
  425. GET_GPIO_P(dir)
  426. GET_GPIO_P(inen)
  427. GET_GPIO_P(polar)
  428. GET_GPIO_P(edge)
  429. GET_GPIO_P(both)
  430. GET_GPIO_P(maska)
  431. GET_GPIO_P(maskb)
  432. #ifdef CONFIG_PM
  433. DECLARE_RESERVED_MAP(wakeup, GPIO_BANK_NUM);
  434. static const unsigned int sic_iwr_irqs[] = {
  435. #if defined(BF533_FAMILY)
  436. IRQ_PROG_INTB
  437. #elif defined(BF537_FAMILY)
  438. IRQ_PF_INTB_WATCH, IRQ_PORTG_INTB, IRQ_PH_INTB_MAC_TX
  439. #elif defined(BF538_FAMILY)
  440. IRQ_PORTF_INTB
  441. #elif defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  442. IRQ_PORTF_INTB, IRQ_PORTG_INTB, IRQ_PORTH_INTB
  443. #elif defined(BF561_FAMILY)
  444. IRQ_PROG0_INTB, IRQ_PROG1_INTB, IRQ_PROG2_INTB
  445. #else
  446. # error no SIC_IWR defined
  447. #endif
  448. };
  449. /***********************************************************
  450. *
  451. * FUNCTIONS: Blackfin PM Setup API
  452. *
  453. * INPUTS/OUTPUTS:
  454. * gpio - GPIO Number between 0 and MAX_BLACKFIN_GPIOS
  455. * type -
  456. * PM_WAKE_RISING
  457. * PM_WAKE_FALLING
  458. * PM_WAKE_HIGH
  459. * PM_WAKE_LOW
  460. * PM_WAKE_BOTH_EDGES
  461. *
  462. * DESCRIPTION: Blackfin PM Driver API
  463. *
  464. * CAUTION:
  465. *************************************************************
  466. * MODIFICATION HISTORY :
  467. **************************************************************/
  468. int bfin_gpio_pm_wakeup_ctrl(unsigned gpio, unsigned ctrl)
  469. {
  470. unsigned long flags;
  471. if (check_gpio(gpio) < 0)
  472. return -EINVAL;
  473. flags = hard_local_irq_save();
  474. if (ctrl)
  475. reserve(wakeup, gpio);
  476. else
  477. unreserve(wakeup, gpio);
  478. set_gpio_maskb(gpio, ctrl);
  479. hard_local_irq_restore(flags);
  480. return 0;
  481. }
  482. int bfin_gpio_pm_standby_ctrl(unsigned ctrl)
  483. {
  484. u16 bank, mask, i;
  485. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  486. mask = map_entry(wakeup, i);
  487. bank = gpio_bank(i);
  488. if (mask)
  489. bfin_internal_set_wake(sic_iwr_irqs[bank], ctrl);
  490. }
  491. return 0;
  492. }
  493. void bfin_gpio_pm_hibernate_suspend(void)
  494. {
  495. int i, bank;
  496. #ifdef BF538_FAMILY
  497. for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
  498. port_fer_saved[i] = *port_fer[i];
  499. #endif
  500. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  501. bank = gpio_bank(i);
  502. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  503. gpio_bank_saved[bank].fer = *port_fer[bank];
  504. #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  505. gpio_bank_saved[bank].mux = *port_mux[bank];
  506. #else
  507. if (bank == 0)
  508. gpio_bank_saved[bank].mux = bfin_read_PORT_MUX();
  509. #endif
  510. #endif
  511. gpio_bank_saved[bank].data = gpio_array[bank]->data;
  512. gpio_bank_saved[bank].inen = gpio_array[bank]->inen;
  513. gpio_bank_saved[bank].polar = gpio_array[bank]->polar;
  514. gpio_bank_saved[bank].dir = gpio_array[bank]->dir;
  515. gpio_bank_saved[bank].edge = gpio_array[bank]->edge;
  516. gpio_bank_saved[bank].both = gpio_array[bank]->both;
  517. gpio_bank_saved[bank].maska = gpio_array[bank]->maska;
  518. }
  519. #ifdef BFIN_SPECIAL_GPIO_BANKS
  520. bfin_special_gpio_pm_hibernate_suspend();
  521. #endif
  522. AWA_DUMMY_READ(maska);
  523. }
  524. void bfin_gpio_pm_hibernate_restore(void)
  525. {
  526. int i, bank;
  527. #ifdef BF538_FAMILY
  528. for (i = 0; i < ARRAY_SIZE(port_fer_saved); ++i)
  529. *port_fer[i] = port_fer_saved[i];
  530. #endif
  531. for (i = 0; i < MAX_BLACKFIN_GPIOS; i += GPIO_BANKSIZE) {
  532. bank = gpio_bank(i);
  533. #if defined(CONFIG_BF52x) || defined(BF537_FAMILY) || defined(CONFIG_BF51x)
  534. #if defined(CONFIG_BF52x) || defined(CONFIG_BF51x)
  535. *port_mux[bank] = gpio_bank_saved[bank].mux;
  536. #else
  537. if (bank == 0)
  538. bfin_write_PORT_MUX(gpio_bank_saved[bank].mux);
  539. #endif
  540. *port_fer[bank] = gpio_bank_saved[bank].fer;
  541. #endif
  542. gpio_array[bank]->inen = gpio_bank_saved[bank].inen;
  543. gpio_array[bank]->data_set = gpio_bank_saved[bank].data
  544. & gpio_bank_saved[bank].dir;
  545. gpio_array[bank]->dir = gpio_bank_saved[bank].dir;
  546. gpio_array[bank]->polar = gpio_bank_saved[bank].polar;
  547. gpio_array[bank]->edge = gpio_bank_saved[bank].edge;
  548. gpio_array[bank]->both = gpio_bank_saved[bank].both;
  549. gpio_array[bank]->maska = gpio_bank_saved[bank].maska;
  550. }
  551. #ifdef BFIN_SPECIAL_GPIO_BANKS
  552. bfin_special_gpio_pm_hibernate_restore();
  553. #endif
  554. AWA_DUMMY_READ(maska);
  555. }
  556. #endif
  557. /***********************************************************
  558. *
  559. * FUNCTIONS: Blackfin Peripheral Resource Allocation
  560. * and PortMux Setup
  561. *
  562. * INPUTS/OUTPUTS:
  563. * per Peripheral Identifier
  564. * label String
  565. *
  566. * DESCRIPTION: Blackfin Peripheral Resource Allocation and Setup API
  567. *
  568. * CAUTION:
  569. *************************************************************
  570. * MODIFICATION HISTORY :
  571. **************************************************************/
  572. int peripheral_request(unsigned short per, const char *label)
  573. {
  574. unsigned long flags;
  575. unsigned short ident = P_IDENT(per);
  576. /*
  577. * Don't cares are pins with only one dedicated function
  578. */
  579. if (per & P_DONTCARE)
  580. return 0;
  581. if (!(per & P_DEFINED))
  582. return -ENODEV;
  583. BUG_ON(ident >= MAX_RESOURCES);
  584. flags = hard_local_irq_save();
  585. /* If a pin can be muxed as either GPIO or peripheral, make
  586. * sure it is not already a GPIO pin when we request it.
  587. */
  588. if (unlikely(!check_gpio(ident) && is_reserved(gpio, ident, 1))) {
  589. if (system_state == SYSTEM_BOOTING)
  590. dump_stack();
  591. printk(KERN_ERR
  592. "%s: Peripheral %d is already reserved as GPIO by %s !\n",
  593. __func__, ident, get_label(ident));
  594. hard_local_irq_restore(flags);
  595. return -EBUSY;
  596. }
  597. if (unlikely(is_reserved(peri, ident, 1))) {
  598. /*
  599. * Pin functions like AMC address strobes my
  600. * be requested and used by several drivers
  601. */
  602. if (!(per & P_MAYSHARE)) {
  603. /*
  604. * Allow that the identical pin function can
  605. * be requested from the same driver twice
  606. */
  607. if (cmp_label(ident, label) == 0)
  608. goto anyway;
  609. if (system_state == SYSTEM_BOOTING)
  610. dump_stack();
  611. printk(KERN_ERR
  612. "%s: Peripheral %d function %d is already reserved by %s !\n",
  613. __func__, ident, P_FUNCT2MUX(per), get_label(ident));
  614. hard_local_irq_restore(flags);
  615. return -EBUSY;
  616. }
  617. }
  618. if (unlikely(portmux_group_check(per))) {
  619. hard_local_irq_restore(flags);
  620. return -EBUSY;
  621. }
  622. anyway:
  623. reserve(peri, ident);
  624. portmux_setup(per);
  625. port_setup(ident, PERIPHERAL_USAGE);
  626. hard_local_irq_restore(flags);
  627. set_label(ident, label);
  628. return 0;
  629. }
  630. EXPORT_SYMBOL(peripheral_request);
  631. int peripheral_request_list(const unsigned short per[], const char *label)
  632. {
  633. u16 cnt;
  634. int ret;
  635. for (cnt = 0; per[cnt] != 0; cnt++) {
  636. ret = peripheral_request(per[cnt], label);
  637. if (ret < 0) {
  638. for ( ; cnt > 0; cnt--)
  639. peripheral_free(per[cnt - 1]);
  640. return ret;
  641. }
  642. }
  643. return 0;
  644. }
  645. EXPORT_SYMBOL(peripheral_request_list);
  646. void peripheral_free(unsigned short per)
  647. {
  648. unsigned long flags;
  649. unsigned short ident = P_IDENT(per);
  650. if (per & P_DONTCARE)
  651. return;
  652. if (!(per & P_DEFINED))
  653. return;
  654. flags = hard_local_irq_save();
  655. if (unlikely(!is_reserved(peri, ident, 0))) {
  656. hard_local_irq_restore(flags);
  657. return;
  658. }
  659. if (!(per & P_MAYSHARE))
  660. port_setup(ident, GPIO_USAGE);
  661. unreserve(peri, ident);
  662. set_label(ident, "free");
  663. hard_local_irq_restore(flags);
  664. }
  665. EXPORT_SYMBOL(peripheral_free);
  666. void peripheral_free_list(const unsigned short per[])
  667. {
  668. u16 cnt;
  669. for (cnt = 0; per[cnt] != 0; cnt++)
  670. peripheral_free(per[cnt]);
  671. }
  672. EXPORT_SYMBOL(peripheral_free_list);
  673. /***********************************************************
  674. *
  675. * FUNCTIONS: Blackfin GPIO Driver
  676. *
  677. * INPUTS/OUTPUTS:
  678. * gpio PIO Number between 0 and MAX_BLACKFIN_GPIOS
  679. * label String
  680. *
  681. * DESCRIPTION: Blackfin GPIO Driver API
  682. *
  683. * CAUTION:
  684. *************************************************************
  685. * MODIFICATION HISTORY :
  686. **************************************************************/
  687. int bfin_gpio_request(unsigned gpio, const char *label)
  688. {
  689. unsigned long flags;
  690. if (check_gpio(gpio) < 0)
  691. return -EINVAL;
  692. flags = hard_local_irq_save();
  693. /*
  694. * Allow that the identical GPIO can
  695. * be requested from the same driver twice
  696. * Do nothing and return -
  697. */
  698. if (cmp_label(gpio, label) == 0) {
  699. hard_local_irq_restore(flags);
  700. return 0;
  701. }
  702. if (unlikely(is_reserved(gpio, gpio, 1))) {
  703. if (system_state == SYSTEM_BOOTING)
  704. dump_stack();
  705. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  706. gpio, get_label(gpio));
  707. hard_local_irq_restore(flags);
  708. return -EBUSY;
  709. }
  710. if (unlikely(is_reserved(peri, gpio, 1))) {
  711. if (system_state == SYSTEM_BOOTING)
  712. dump_stack();
  713. printk(KERN_ERR
  714. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  715. gpio, get_label(gpio));
  716. hard_local_irq_restore(flags);
  717. return -EBUSY;
  718. }
  719. if (unlikely(is_reserved(gpio_irq, gpio, 1))) {
  720. printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved as gpio-irq!"
  721. " (Documentation/blackfin/bfin-gpio-notes.txt)\n", gpio);
  722. } else { /* Reset POLAR setting when acquiring a gpio for the first time */
  723. set_gpio_polar(gpio, 0);
  724. }
  725. reserve(gpio, gpio);
  726. set_label(gpio, label);
  727. hard_local_irq_restore(flags);
  728. port_setup(gpio, GPIO_USAGE);
  729. return 0;
  730. }
  731. EXPORT_SYMBOL(bfin_gpio_request);
  732. void bfin_gpio_free(unsigned gpio)
  733. {
  734. unsigned long flags;
  735. if (check_gpio(gpio) < 0)
  736. return;
  737. might_sleep();
  738. flags = hard_local_irq_save();
  739. if (unlikely(!is_reserved(gpio, gpio, 0))) {
  740. if (system_state == SYSTEM_BOOTING)
  741. dump_stack();
  742. gpio_error(gpio);
  743. hard_local_irq_restore(flags);
  744. return;
  745. }
  746. unreserve(gpio, gpio);
  747. set_label(gpio, "free");
  748. hard_local_irq_restore(flags);
  749. }
  750. EXPORT_SYMBOL(bfin_gpio_free);
  751. #ifdef BFIN_SPECIAL_GPIO_BANKS
  752. DECLARE_RESERVED_MAP(special_gpio, gpio_bank(MAX_RESOURCES));
  753. int bfin_special_gpio_request(unsigned gpio, const char *label)
  754. {
  755. unsigned long flags;
  756. flags = hard_local_irq_save();
  757. /*
  758. * Allow that the identical GPIO can
  759. * be requested from the same driver twice
  760. * Do nothing and return -
  761. */
  762. if (cmp_label(gpio, label) == 0) {
  763. hard_local_irq_restore(flags);
  764. return 0;
  765. }
  766. if (unlikely(is_reserved(special_gpio, gpio, 1))) {
  767. hard_local_irq_restore(flags);
  768. printk(KERN_ERR "bfin-gpio: GPIO %d is already reserved by %s !\n",
  769. gpio, get_label(gpio));
  770. return -EBUSY;
  771. }
  772. if (unlikely(is_reserved(peri, gpio, 1))) {
  773. hard_local_irq_restore(flags);
  774. printk(KERN_ERR
  775. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  776. gpio, get_label(gpio));
  777. return -EBUSY;
  778. }
  779. reserve(special_gpio, gpio);
  780. reserve(peri, gpio);
  781. set_label(gpio, label);
  782. hard_local_irq_restore(flags);
  783. port_setup(gpio, GPIO_USAGE);
  784. return 0;
  785. }
  786. EXPORT_SYMBOL(bfin_special_gpio_request);
  787. void bfin_special_gpio_free(unsigned gpio)
  788. {
  789. unsigned long flags;
  790. might_sleep();
  791. flags = hard_local_irq_save();
  792. if (unlikely(!is_reserved(special_gpio, gpio, 0))) {
  793. gpio_error(gpio);
  794. hard_local_irq_restore(flags);
  795. return;
  796. }
  797. unreserve(special_gpio, gpio);
  798. unreserve(peri, gpio);
  799. set_label(gpio, "free");
  800. hard_local_irq_restore(flags);
  801. }
  802. EXPORT_SYMBOL(bfin_special_gpio_free);
  803. #endif
  804. int bfin_gpio_irq_request(unsigned gpio, const char *label)
  805. {
  806. unsigned long flags;
  807. if (check_gpio(gpio) < 0)
  808. return -EINVAL;
  809. flags = hard_local_irq_save();
  810. if (unlikely(is_reserved(peri, gpio, 1))) {
  811. if (system_state == SYSTEM_BOOTING)
  812. dump_stack();
  813. printk(KERN_ERR
  814. "bfin-gpio: GPIO %d is already reserved as Peripheral by %s !\n",
  815. gpio, get_label(gpio));
  816. hard_local_irq_restore(flags);
  817. return -EBUSY;
  818. }
  819. if (unlikely(is_reserved(gpio, gpio, 1)))
  820. printk(KERN_NOTICE "bfin-gpio: GPIO %d is already reserved by %s! "
  821. "(Documentation/blackfin/bfin-gpio-notes.txt)\n",
  822. gpio, get_label(gpio));
  823. reserve(gpio_irq, gpio);
  824. set_label(gpio, label);
  825. hard_local_irq_restore(flags);
  826. port_setup(gpio, GPIO_USAGE);
  827. return 0;
  828. }
  829. void bfin_gpio_irq_free(unsigned gpio)
  830. {
  831. unsigned long flags;
  832. if (check_gpio(gpio) < 0)
  833. return;
  834. flags = hard_local_irq_save();
  835. if (unlikely(!is_reserved(gpio_irq, gpio, 0))) {
  836. if (system_state == SYSTEM_BOOTING)
  837. dump_stack();
  838. gpio_error(gpio);
  839. hard_local_irq_restore(flags);
  840. return;
  841. }
  842. unreserve(gpio_irq, gpio);
  843. set_label(gpio, "free");
  844. hard_local_irq_restore(flags);
  845. }
  846. static inline void __bfin_gpio_direction_input(unsigned gpio)
  847. {
  848. gpio_array[gpio_bank(gpio)]->dir &= ~gpio_bit(gpio);
  849. gpio_array[gpio_bank(gpio)]->inen |= gpio_bit(gpio);
  850. }
  851. int bfin_gpio_direction_input(unsigned gpio)
  852. {
  853. unsigned long flags;
  854. if (unlikely(!is_reserved(gpio, gpio, 0))) {
  855. gpio_error(gpio);
  856. return -EINVAL;
  857. }
  858. flags = hard_local_irq_save();
  859. __bfin_gpio_direction_input(gpio);
  860. AWA_DUMMY_READ(inen);
  861. hard_local_irq_restore(flags);
  862. return 0;
  863. }
  864. EXPORT_SYMBOL(bfin_gpio_direction_input);
  865. void bfin_gpio_irq_prepare(unsigned gpio)
  866. {
  867. port_setup(gpio, GPIO_USAGE);
  868. }
  869. void bfin_gpio_set_value(unsigned gpio, int arg)
  870. {
  871. if (arg)
  872. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  873. else
  874. gpio_array[gpio_bank(gpio)]->data_clear = gpio_bit(gpio);
  875. }
  876. EXPORT_SYMBOL(bfin_gpio_set_value);
  877. int bfin_gpio_direction_output(unsigned gpio, int value)
  878. {
  879. unsigned long flags;
  880. if (unlikely(!is_reserved(gpio, gpio, 0))) {
  881. gpio_error(gpio);
  882. return -EINVAL;
  883. }
  884. flags = hard_local_irq_save();
  885. gpio_array[gpio_bank(gpio)]->inen &= ~gpio_bit(gpio);
  886. gpio_set_value(gpio, value);
  887. gpio_array[gpio_bank(gpio)]->dir |= gpio_bit(gpio);
  888. AWA_DUMMY_READ(dir);
  889. hard_local_irq_restore(flags);
  890. return 0;
  891. }
  892. EXPORT_SYMBOL(bfin_gpio_direction_output);
  893. int bfin_gpio_get_value(unsigned gpio)
  894. {
  895. unsigned long flags;
  896. if (unlikely(get_gpio_edge(gpio))) {
  897. int ret;
  898. flags = hard_local_irq_save();
  899. set_gpio_edge(gpio, 0);
  900. ret = get_gpio_data(gpio);
  901. set_gpio_edge(gpio, 1);
  902. hard_local_irq_restore(flags);
  903. return ret;
  904. } else
  905. return get_gpio_data(gpio);
  906. }
  907. EXPORT_SYMBOL(bfin_gpio_get_value);
  908. /* If we are booting from SPI and our board lacks a strong enough pull up,
  909. * the core can reset and execute the bootrom faster than the resistor can
  910. * pull the signal logically high. To work around this (common) error in
  911. * board design, we explicitly set the pin back to GPIO mode, force /CS
  912. * high, and wait for the electrons to do their thing.
  913. *
  914. * This function only makes sense to be called from reset code, but it
  915. * lives here as we need to force all the GPIO states w/out going through
  916. * BUG() checks and such.
  917. */
  918. void bfin_reset_boot_spi_cs(unsigned short pin)
  919. {
  920. unsigned short gpio = P_IDENT(pin);
  921. port_setup(gpio, GPIO_USAGE);
  922. gpio_array[gpio_bank(gpio)]->data_set = gpio_bit(gpio);
  923. AWA_DUMMY_READ(data_set);
  924. udelay(1);
  925. }
  926. #if defined(CONFIG_PROC_FS)
  927. static int gpio_proc_show(struct seq_file *m, void *v)
  928. {
  929. int c, irq, gpio;
  930. for (c = 0; c < MAX_RESOURCES; c++) {
  931. irq = is_reserved(gpio_irq, c, 1);
  932. gpio = is_reserved(gpio, c, 1);
  933. if (!check_gpio(c) && (gpio || irq))
  934. seq_printf(m, "GPIO_%d: \t%s%s \t\tGPIO %s\n", c,
  935. get_label(c), (gpio && irq) ? " *" : "",
  936. get_gpio_dir(c) ? "OUTPUT" : "INPUT");
  937. else if (is_reserved(peri, c, 1))
  938. seq_printf(m, "GPIO_%d: \t%s \t\tPeripheral\n", c, get_label(c));
  939. else
  940. continue;
  941. }
  942. return 0;
  943. }
  944. static int gpio_proc_open(struct inode *inode, struct file *file)
  945. {
  946. return single_open(file, gpio_proc_show, NULL);
  947. }
  948. static const struct file_operations gpio_proc_ops = {
  949. .open = gpio_proc_open,
  950. .read = seq_read,
  951. .llseek = seq_lseek,
  952. .release = single_release,
  953. };
  954. static __init int gpio_register_proc(void)
  955. {
  956. struct proc_dir_entry *proc_gpio;
  957. proc_gpio = proc_create("gpio", 0, NULL, &gpio_proc_ops);
  958. return proc_gpio == NULL;
  959. }
  960. __initcall(gpio_register_proc);
  961. #endif
  962. #ifdef CONFIG_GPIOLIB
  963. static int bfin_gpiolib_direction_input(struct gpio_chip *chip, unsigned gpio)
  964. {
  965. return bfin_gpio_direction_input(gpio);
  966. }
  967. static int bfin_gpiolib_direction_output(struct gpio_chip *chip, unsigned gpio, int level)
  968. {
  969. return bfin_gpio_direction_output(gpio, level);
  970. }
  971. static int bfin_gpiolib_get_value(struct gpio_chip *chip, unsigned gpio)
  972. {
  973. return !!bfin_gpio_get_value(gpio);
  974. }
  975. static void bfin_gpiolib_set_value(struct gpio_chip *chip, unsigned gpio, int value)
  976. {
  977. return bfin_gpio_set_value(gpio, value);
  978. }
  979. static int bfin_gpiolib_gpio_request(struct gpio_chip *chip, unsigned gpio)
  980. {
  981. return bfin_gpio_request(gpio, chip->label);
  982. }
  983. static void bfin_gpiolib_gpio_free(struct gpio_chip *chip, unsigned gpio)
  984. {
  985. return bfin_gpio_free(gpio);
  986. }
  987. static int bfin_gpiolib_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
  988. {
  989. return gpio + GPIO_IRQ_BASE;
  990. }
  991. static struct gpio_chip bfin_chip = {
  992. .label = "BFIN-GPIO",
  993. .direction_input = bfin_gpiolib_direction_input,
  994. .get = bfin_gpiolib_get_value,
  995. .direction_output = bfin_gpiolib_direction_output,
  996. .set = bfin_gpiolib_set_value,
  997. .request = bfin_gpiolib_gpio_request,
  998. .free = bfin_gpiolib_gpio_free,
  999. .to_irq = bfin_gpiolib_gpio_to_irq,
  1000. .base = 0,
  1001. .ngpio = MAX_BLACKFIN_GPIOS,
  1002. };
  1003. static int __init bfin_gpiolib_setup(void)
  1004. {
  1005. return gpiochip_add_data(&bfin_chip, NULL);
  1006. }
  1007. arch_initcall(bfin_gpiolib_setup);
  1008. #endif