gpmc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/clk.h>
  21. #include <linux/ioport.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <asm/mach-types.h>
  27. #include <plat/gpmc.h>
  28. #include <plat/sdrc.h>
  29. /* GPMC register offsets */
  30. #define GPMC_REVISION 0x00
  31. #define GPMC_SYSCONFIG 0x10
  32. #define GPMC_SYSSTATUS 0x14
  33. #define GPMC_IRQSTATUS 0x18
  34. #define GPMC_IRQENABLE 0x1c
  35. #define GPMC_TIMEOUT_CONTROL 0x40
  36. #define GPMC_ERR_ADDRESS 0x44
  37. #define GPMC_ERR_TYPE 0x48
  38. #define GPMC_CONFIG 0x50
  39. #define GPMC_STATUS 0x54
  40. #define GPMC_PREFETCH_CONFIG1 0x1e0
  41. #define GPMC_PREFETCH_CONFIG2 0x1e4
  42. #define GPMC_PREFETCH_CONTROL 0x1ec
  43. #define GPMC_PREFETCH_STATUS 0x1f0
  44. #define GPMC_ECC_CONFIG 0x1f4
  45. #define GPMC_ECC_CONTROL 0x1f8
  46. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  47. #define GPMC_ECC1_RESULT 0x200
  48. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  49. /* GPMC ECC control settings */
  50. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  51. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  52. #define GPMC_ECC_CTRL_ECCREG1 0x001
  53. #define GPMC_ECC_CTRL_ECCREG2 0x002
  54. #define GPMC_ECC_CTRL_ECCREG3 0x003
  55. #define GPMC_ECC_CTRL_ECCREG4 0x004
  56. #define GPMC_ECC_CTRL_ECCREG5 0x005
  57. #define GPMC_ECC_CTRL_ECCREG6 0x006
  58. #define GPMC_ECC_CTRL_ECCREG7 0x007
  59. #define GPMC_ECC_CTRL_ECCREG8 0x008
  60. #define GPMC_ECC_CTRL_ECCREG9 0x009
  61. #define GPMC_CS0_OFFSET 0x60
  62. #define GPMC_CS_SIZE 0x30
  63. #define GPMC_MEM_START 0x00000000
  64. #define GPMC_MEM_END 0x3FFFFFFF
  65. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  66. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  67. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  68. #define CS_NUM_SHIFT 24
  69. #define ENABLE_PREFETCH (0x1 << 7)
  70. #define DMA_MPU_MODE 2
  71. /* Structure to save gpmc cs context */
  72. struct gpmc_cs_config {
  73. u32 config1;
  74. u32 config2;
  75. u32 config3;
  76. u32 config4;
  77. u32 config5;
  78. u32 config6;
  79. u32 config7;
  80. int is_valid;
  81. };
  82. /*
  83. * Structure to save/restore gpmc context
  84. * to support core off on OMAP3
  85. */
  86. struct omap3_gpmc_regs {
  87. u32 sysconfig;
  88. u32 irqenable;
  89. u32 timeout_ctrl;
  90. u32 config;
  91. u32 prefetch_config1;
  92. u32 prefetch_config2;
  93. u32 prefetch_control;
  94. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  95. };
  96. static struct resource gpmc_mem_root;
  97. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  98. static DEFINE_SPINLOCK(gpmc_mem_lock);
  99. static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
  100. static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */
  101. static void __iomem *gpmc_base;
  102. static struct clk *gpmc_l3_clk;
  103. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  104. static void gpmc_write_reg(int idx, u32 val)
  105. {
  106. __raw_writel(val, gpmc_base + idx);
  107. }
  108. static u32 gpmc_read_reg(int idx)
  109. {
  110. return __raw_readl(gpmc_base + idx);
  111. }
  112. static void gpmc_cs_write_byte(int cs, int idx, u8 val)
  113. {
  114. void __iomem *reg_addr;
  115. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  116. __raw_writeb(val, reg_addr);
  117. }
  118. static u8 gpmc_cs_read_byte(int cs, int idx)
  119. {
  120. void __iomem *reg_addr;
  121. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  122. return __raw_readb(reg_addr);
  123. }
  124. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  125. {
  126. void __iomem *reg_addr;
  127. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  128. __raw_writel(val, reg_addr);
  129. }
  130. u32 gpmc_cs_read_reg(int cs, int idx)
  131. {
  132. void __iomem *reg_addr;
  133. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  134. return __raw_readl(reg_addr);
  135. }
  136. /* TODO: Add support for gpmc_fck to clock framework and use it */
  137. unsigned long gpmc_get_fclk_period(void)
  138. {
  139. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  140. if (rate == 0) {
  141. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  142. return 0;
  143. }
  144. rate /= 1000;
  145. rate = 1000000000 / rate; /* In picoseconds */
  146. return rate;
  147. }
  148. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  149. {
  150. unsigned long tick_ps;
  151. /* Calculate in picosecs to yield more exact results */
  152. tick_ps = gpmc_get_fclk_period();
  153. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  154. }
  155. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  156. {
  157. unsigned long tick_ps;
  158. /* Calculate in picosecs to yield more exact results */
  159. tick_ps = gpmc_get_fclk_period();
  160. return (time_ps + tick_ps - 1) / tick_ps;
  161. }
  162. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  163. {
  164. return ticks * gpmc_get_fclk_period() / 1000;
  165. }
  166. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  167. {
  168. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  169. return ticks * gpmc_get_fclk_period() / 1000;
  170. }
  171. #ifdef DEBUG
  172. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  173. int time, const char *name)
  174. #else
  175. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  176. int time)
  177. #endif
  178. {
  179. u32 l;
  180. int ticks, mask, nr_bits;
  181. if (time == 0)
  182. ticks = 0;
  183. else
  184. ticks = gpmc_ns_to_ticks(time);
  185. nr_bits = end_bit - st_bit + 1;
  186. if (ticks >= 1 << nr_bits) {
  187. #ifdef DEBUG
  188. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  189. cs, name, time, ticks, 1 << nr_bits);
  190. #endif
  191. return -1;
  192. }
  193. mask = (1 << nr_bits) - 1;
  194. l = gpmc_cs_read_reg(cs, reg);
  195. #ifdef DEBUG
  196. printk(KERN_INFO
  197. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  198. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  199. (l >> st_bit) & mask, time);
  200. #endif
  201. l &= ~(mask << st_bit);
  202. l |= ticks << st_bit;
  203. gpmc_cs_write_reg(cs, reg, l);
  204. return 0;
  205. }
  206. #ifdef DEBUG
  207. #define GPMC_SET_ONE(reg, st, end, field) \
  208. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  209. t->field, #field) < 0) \
  210. return -1
  211. #else
  212. #define GPMC_SET_ONE(reg, st, end, field) \
  213. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  214. return -1
  215. #endif
  216. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  217. {
  218. int div;
  219. u32 l;
  220. l = sync_clk + (gpmc_get_fclk_period() - 1);
  221. div = l / gpmc_get_fclk_period();
  222. if (div > 4)
  223. return -1;
  224. if (div <= 0)
  225. div = 1;
  226. return div;
  227. }
  228. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  229. {
  230. int div;
  231. u32 l;
  232. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  233. if (div < 0)
  234. return -1;
  235. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  236. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  237. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  238. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  239. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  240. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  241. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  242. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  243. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  244. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  245. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  246. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  247. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  248. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  249. if (cpu_is_omap34xx()) {
  250. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  251. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  252. }
  253. /* caller is expected to have initialized CONFIG1 to cover
  254. * at least sync vs async
  255. */
  256. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  257. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  258. #ifdef DEBUG
  259. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  260. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  261. #endif
  262. l &= ~0x03;
  263. l |= (div - 1);
  264. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  265. }
  266. return 0;
  267. }
  268. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  269. {
  270. u32 l;
  271. u32 mask;
  272. mask = (1 << GPMC_SECTION_SHIFT) - size;
  273. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  274. l &= ~0x3f;
  275. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  276. l &= ~(0x0f << 8);
  277. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  278. l |= GPMC_CONFIG7_CSVALID;
  279. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  280. }
  281. static void gpmc_cs_disable_mem(int cs)
  282. {
  283. u32 l;
  284. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  285. l &= ~GPMC_CONFIG7_CSVALID;
  286. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  287. }
  288. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  289. {
  290. u32 l;
  291. u32 mask;
  292. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  293. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  294. mask = (l >> 8) & 0x0f;
  295. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  296. }
  297. static int gpmc_cs_mem_enabled(int cs)
  298. {
  299. u32 l;
  300. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  301. return l & GPMC_CONFIG7_CSVALID;
  302. }
  303. int gpmc_cs_set_reserved(int cs, int reserved)
  304. {
  305. if (cs > GPMC_CS_NUM)
  306. return -ENODEV;
  307. gpmc_cs_map &= ~(1 << cs);
  308. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  309. return 0;
  310. }
  311. int gpmc_cs_reserved(int cs)
  312. {
  313. if (cs > GPMC_CS_NUM)
  314. return -ENODEV;
  315. return gpmc_cs_map & (1 << cs);
  316. }
  317. static unsigned long gpmc_mem_align(unsigned long size)
  318. {
  319. int order;
  320. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  321. order = GPMC_CHUNK_SHIFT - 1;
  322. do {
  323. size >>= 1;
  324. order++;
  325. } while (size);
  326. size = 1 << order;
  327. return size;
  328. }
  329. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  330. {
  331. struct resource *res = &gpmc_cs_mem[cs];
  332. int r;
  333. size = gpmc_mem_align(size);
  334. spin_lock(&gpmc_mem_lock);
  335. res->start = base;
  336. res->end = base + size - 1;
  337. r = request_resource(&gpmc_mem_root, res);
  338. spin_unlock(&gpmc_mem_lock);
  339. return r;
  340. }
  341. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  342. {
  343. struct resource *res = &gpmc_cs_mem[cs];
  344. int r = -1;
  345. if (cs > GPMC_CS_NUM)
  346. return -ENODEV;
  347. size = gpmc_mem_align(size);
  348. if (size > (1 << GPMC_SECTION_SHIFT))
  349. return -ENOMEM;
  350. spin_lock(&gpmc_mem_lock);
  351. if (gpmc_cs_reserved(cs)) {
  352. r = -EBUSY;
  353. goto out;
  354. }
  355. if (gpmc_cs_mem_enabled(cs))
  356. r = adjust_resource(res, res->start & ~(size - 1), size);
  357. if (r < 0)
  358. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  359. size, NULL, NULL);
  360. if (r < 0)
  361. goto out;
  362. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  363. *base = res->start;
  364. gpmc_cs_set_reserved(cs, 1);
  365. out:
  366. spin_unlock(&gpmc_mem_lock);
  367. return r;
  368. }
  369. EXPORT_SYMBOL(gpmc_cs_request);
  370. void gpmc_cs_free(int cs)
  371. {
  372. spin_lock(&gpmc_mem_lock);
  373. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  374. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  375. BUG();
  376. spin_unlock(&gpmc_mem_lock);
  377. return;
  378. }
  379. gpmc_cs_disable_mem(cs);
  380. release_resource(&gpmc_cs_mem[cs]);
  381. gpmc_cs_set_reserved(cs, 0);
  382. spin_unlock(&gpmc_mem_lock);
  383. }
  384. EXPORT_SYMBOL(gpmc_cs_free);
  385. /**
  386. * gpmc_read_status - read access request to get the different gpmc status
  387. * @cmd: command type
  388. * @return status
  389. */
  390. int gpmc_read_status(int cmd)
  391. {
  392. int status = -EINVAL;
  393. u32 regval = 0;
  394. switch (cmd) {
  395. case GPMC_GET_IRQ_STATUS:
  396. status = gpmc_read_reg(GPMC_IRQSTATUS);
  397. break;
  398. case GPMC_PREFETCH_FIFO_CNT:
  399. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  400. status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval);
  401. break;
  402. case GPMC_PREFETCH_COUNT:
  403. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  404. status = GPMC_PREFETCH_STATUS_COUNT(regval);
  405. break;
  406. case GPMC_STATUS_BUFFER:
  407. regval = gpmc_read_reg(GPMC_STATUS);
  408. /* 1 : buffer is available to write */
  409. status = regval & GPMC_STATUS_BUFF_EMPTY;
  410. break;
  411. default:
  412. printk(KERN_ERR "gpmc_read_status: Not supported\n");
  413. }
  414. return status;
  415. }
  416. EXPORT_SYMBOL(gpmc_read_status);
  417. /**
  418. * gpmc_cs_configure - write request to configure gpmc
  419. * @cs: chip select number
  420. * @cmd: command type
  421. * @wval: value to write
  422. * @return status of the operation
  423. */
  424. int gpmc_cs_configure(int cs, int cmd, int wval)
  425. {
  426. int err = 0;
  427. u32 regval = 0;
  428. switch (cmd) {
  429. case GPMC_ENABLE_IRQ:
  430. gpmc_write_reg(GPMC_IRQENABLE, wval);
  431. break;
  432. case GPMC_SET_IRQ_STATUS:
  433. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  434. break;
  435. case GPMC_CONFIG_WP:
  436. regval = gpmc_read_reg(GPMC_CONFIG);
  437. if (wval)
  438. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  439. else
  440. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  441. gpmc_write_reg(GPMC_CONFIG, regval);
  442. break;
  443. case GPMC_CONFIG_RDY_BSY:
  444. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  445. if (wval)
  446. regval |= WR_RD_PIN_MONITORING;
  447. else
  448. regval &= ~WR_RD_PIN_MONITORING;
  449. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  450. break;
  451. case GPMC_CONFIG_DEV_SIZE:
  452. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  453. /* clear 2 target bits */
  454. regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
  455. /* set the proper value */
  456. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  457. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  458. break;
  459. case GPMC_CONFIG_DEV_TYPE:
  460. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  461. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  462. if (wval == GPMC_DEVICETYPE_NOR)
  463. regval |= GPMC_CONFIG1_MUXADDDATA;
  464. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  465. break;
  466. default:
  467. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  468. err = -EINVAL;
  469. }
  470. return err;
  471. }
  472. EXPORT_SYMBOL(gpmc_cs_configure);
  473. /**
  474. * gpmc_nand_read - nand specific read access request
  475. * @cs: chip select number
  476. * @cmd: command type
  477. */
  478. int gpmc_nand_read(int cs, int cmd)
  479. {
  480. int rval = -EINVAL;
  481. switch (cmd) {
  482. case GPMC_NAND_DATA:
  483. rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
  484. break;
  485. default:
  486. printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
  487. }
  488. return rval;
  489. }
  490. EXPORT_SYMBOL(gpmc_nand_read);
  491. /**
  492. * gpmc_nand_write - nand specific write request
  493. * @cs: chip select number
  494. * @cmd: command type
  495. * @wval: value to write
  496. */
  497. int gpmc_nand_write(int cs, int cmd, int wval)
  498. {
  499. int err = 0;
  500. switch (cmd) {
  501. case GPMC_NAND_COMMAND:
  502. gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
  503. break;
  504. case GPMC_NAND_ADDRESS:
  505. gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
  506. break;
  507. case GPMC_NAND_DATA:
  508. gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
  509. default:
  510. printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
  511. err = -EINVAL;
  512. }
  513. return err;
  514. }
  515. EXPORT_SYMBOL(gpmc_nand_write);
  516. /**
  517. * gpmc_prefetch_enable - configures and starts prefetch transfer
  518. * @cs: cs (chip select) number
  519. * @fifo_th: fifo threshold to be used for read/ write
  520. * @dma_mode: dma mode enable (1) or disable (0)
  521. * @u32_count: number of bytes to be transferred
  522. * @is_write: prefetch read(0) or write post(1) mode
  523. */
  524. int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode,
  525. unsigned int u32_count, int is_write)
  526. {
  527. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) {
  528. pr_err("gpmc: fifo threshold is not supported\n");
  529. return -1;
  530. } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
  531. /* Set the amount of bytes to be prefetched */
  532. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
  533. /* Set dma/mpu mode, the prefetch read / post write and
  534. * enable the engine. Set which cs is has requested for.
  535. */
  536. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
  537. PREFETCH_FIFOTHRESHOLD(fifo_th) |
  538. ENABLE_PREFETCH |
  539. (dma_mode << DMA_MPU_MODE) |
  540. (0x1 & is_write)));
  541. /* Start the prefetch engine */
  542. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
  543. } else {
  544. return -EBUSY;
  545. }
  546. return 0;
  547. }
  548. EXPORT_SYMBOL(gpmc_prefetch_enable);
  549. /**
  550. * gpmc_prefetch_reset - disables and stops the prefetch engine
  551. */
  552. int gpmc_prefetch_reset(int cs)
  553. {
  554. u32 config1;
  555. /* check if the same module/cs is trying to reset */
  556. config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  557. if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
  558. return -EINVAL;
  559. /* Stop the PFPW engine */
  560. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
  561. /* Reset/disable the PFPW engine */
  562. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
  563. return 0;
  564. }
  565. EXPORT_SYMBOL(gpmc_prefetch_reset);
  566. static void __init gpmc_mem_init(void)
  567. {
  568. int cs;
  569. unsigned long boot_rom_space = 0;
  570. /* never allocate the first page, to facilitate bug detection;
  571. * even if we didn't boot from ROM.
  572. */
  573. boot_rom_space = BOOT_ROM_SPACE;
  574. /* In apollon the CS0 is mapped as 0x0000 0000 */
  575. if (machine_is_omap_apollon())
  576. boot_rom_space = 0;
  577. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  578. gpmc_mem_root.end = GPMC_MEM_END;
  579. /* Reserve all regions that has been set up by bootloader */
  580. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  581. u32 base, size;
  582. if (!gpmc_cs_mem_enabled(cs))
  583. continue;
  584. gpmc_cs_get_memconf(cs, &base, &size);
  585. if (gpmc_cs_insert_mem(cs, base, size) < 0)
  586. BUG();
  587. }
  588. }
  589. static int __init gpmc_init(void)
  590. {
  591. u32 l, irq;
  592. int cs, ret = -EINVAL;
  593. int gpmc_irq;
  594. char *ck = NULL;
  595. if (cpu_is_omap24xx()) {
  596. ck = "core_l3_ck";
  597. if (cpu_is_omap2420())
  598. l = OMAP2420_GPMC_BASE;
  599. else
  600. l = OMAP34XX_GPMC_BASE;
  601. gpmc_irq = INT_34XX_GPMC_IRQ;
  602. } else if (cpu_is_omap34xx()) {
  603. ck = "gpmc_fck";
  604. l = OMAP34XX_GPMC_BASE;
  605. gpmc_irq = INT_34XX_GPMC_IRQ;
  606. } else if (cpu_is_omap44xx() || soc_is_omap54xx()) {
  607. /* Base address and irq number are same for OMAP4/5 */
  608. ck = "gpmc_ck";
  609. l = OMAP44XX_GPMC_BASE;
  610. gpmc_irq = OMAP44XX_IRQ_GPMC;
  611. }
  612. if (WARN_ON(!ck))
  613. return ret;
  614. gpmc_l3_clk = clk_get(NULL, ck);
  615. if (IS_ERR(gpmc_l3_clk)) {
  616. printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
  617. BUG();
  618. }
  619. gpmc_base = ioremap(l, SZ_4K);
  620. if (!gpmc_base) {
  621. clk_put(gpmc_l3_clk);
  622. printk(KERN_ERR "Could not get GPMC register memory\n");
  623. BUG();
  624. }
  625. clk_enable(gpmc_l3_clk);
  626. l = gpmc_read_reg(GPMC_REVISION);
  627. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  628. /* Set smart idle mode and automatic L3 clock gating */
  629. l = gpmc_read_reg(GPMC_SYSCONFIG);
  630. l &= 0x03 << 3;
  631. l |= (0x02 << 3) | (1 << 0);
  632. gpmc_write_reg(GPMC_SYSCONFIG, l);
  633. gpmc_mem_init();
  634. /* initalize the irq_chained */
  635. irq = OMAP_GPMC_IRQ_BASE;
  636. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  637. irq_set_chip_and_handler(irq, &dummy_irq_chip,
  638. handle_simple_irq);
  639. set_irq_flags(irq, IRQF_VALID);
  640. irq++;
  641. }
  642. ret = request_irq(gpmc_irq, gpmc_handle_irq, IRQF_SHARED, "gpmc", NULL);
  643. if (ret)
  644. pr_err("gpmc: irq-%d could not claim: err %d\n",
  645. gpmc_irq, ret);
  646. return ret;
  647. }
  648. postcore_initcall(gpmc_init);
  649. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  650. {
  651. u8 cs;
  652. /* check cs to invoke the irq */
  653. cs = ((gpmc_read_reg(GPMC_PREFETCH_CONFIG1)) >> CS_NUM_SHIFT) & 0x7;
  654. if (OMAP_GPMC_IRQ_BASE+cs <= OMAP_GPMC_IRQ_END)
  655. generic_handle_irq(OMAP_GPMC_IRQ_BASE+cs);
  656. return IRQ_HANDLED;
  657. }
  658. #ifdef CONFIG_ARCH_OMAP3
  659. static struct omap3_gpmc_regs gpmc_context;
  660. void omap3_gpmc_save_context(void)
  661. {
  662. int i;
  663. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  664. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  665. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  666. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  667. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  668. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  669. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  670. for (i = 0; i < GPMC_CS_NUM; i++) {
  671. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  672. if (gpmc_context.cs_context[i].is_valid) {
  673. gpmc_context.cs_context[i].config1 =
  674. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  675. gpmc_context.cs_context[i].config2 =
  676. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  677. gpmc_context.cs_context[i].config3 =
  678. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  679. gpmc_context.cs_context[i].config4 =
  680. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  681. gpmc_context.cs_context[i].config5 =
  682. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  683. gpmc_context.cs_context[i].config6 =
  684. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  685. gpmc_context.cs_context[i].config7 =
  686. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  687. }
  688. }
  689. }
  690. void omap3_gpmc_restore_context(void)
  691. {
  692. int i;
  693. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  694. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  695. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  696. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  697. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  698. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  699. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  700. for (i = 0; i < GPMC_CS_NUM; i++) {
  701. if (gpmc_context.cs_context[i].is_valid) {
  702. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  703. gpmc_context.cs_context[i].config1);
  704. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  705. gpmc_context.cs_context[i].config2);
  706. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  707. gpmc_context.cs_context[i].config3);
  708. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  709. gpmc_context.cs_context[i].config4);
  710. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  711. gpmc_context.cs_context[i].config5);
  712. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  713. gpmc_context.cs_context[i].config6);
  714. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  715. gpmc_context.cs_context[i].config7);
  716. }
  717. }
  718. }
  719. #endif /* CONFIG_ARCH_OMAP3 */
  720. /**
  721. * gpmc_enable_hwecc - enable hardware ecc functionality
  722. * @cs: chip select number
  723. * @mode: read/write mode
  724. * @dev_width: device bus width(1 for x16, 0 for x8)
  725. * @ecc_size: bytes for which ECC will be generated
  726. */
  727. int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
  728. {
  729. unsigned int val;
  730. /* check if ecc module is in used */
  731. if (gpmc_ecc_used != -EINVAL)
  732. return -EINVAL;
  733. gpmc_ecc_used = cs;
  734. /* clear ecc and enable bits */
  735. gpmc_write_reg(GPMC_ECC_CONTROL,
  736. GPMC_ECC_CTRL_ECCCLEAR |
  737. GPMC_ECC_CTRL_ECCREG1);
  738. /* program ecc and result sizes */
  739. val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
  740. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
  741. switch (mode) {
  742. case GPMC_ECC_READ:
  743. case GPMC_ECC_WRITE:
  744. gpmc_write_reg(GPMC_ECC_CONTROL,
  745. GPMC_ECC_CTRL_ECCCLEAR |
  746. GPMC_ECC_CTRL_ECCREG1);
  747. break;
  748. case GPMC_ECC_READSYN:
  749. gpmc_write_reg(GPMC_ECC_CONTROL,
  750. GPMC_ECC_CTRL_ECCCLEAR |
  751. GPMC_ECC_CTRL_ECCDISABLE);
  752. break;
  753. default:
  754. printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
  755. break;
  756. }
  757. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  758. val = (dev_width << 7) | (cs << 1) | (0x1);
  759. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  760. return 0;
  761. }
  762. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc);
  763. /**
  764. * gpmc_calculate_ecc - generate non-inverted ecc bytes
  765. * @cs: chip select number
  766. * @dat: data pointer over which ecc is computed
  767. * @ecc_code: ecc code buffer
  768. *
  769. * Using non-inverted ECC is considered ugly since writing a blank
  770. * page (padding) will clear the ECC bytes. This is not a problem as long
  771. * no one is trying to write data on the seemingly unused page. Reading
  772. * an erased page will produce an ECC mismatch between generated and read
  773. * ECC bytes that has to be dealt with separately.
  774. */
  775. int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
  776. {
  777. unsigned int val = 0x0;
  778. if (gpmc_ecc_used != cs)
  779. return -EINVAL;
  780. /* read ecc result */
  781. val = gpmc_read_reg(GPMC_ECC1_RESULT);
  782. *ecc_code++ = val; /* P128e, ..., P1e */
  783. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  784. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  785. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  786. gpmc_ecc_used = -EINVAL;
  787. return 0;
  788. }
  789. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc);
  790. #ifdef CONFIG_ARCH_OMAP3
  791. /**
  792. * gpmc_init_hwecc_bch - initialize hardware BCH ecc functionality
  793. * @cs: chip select number
  794. * @nsectors: how many 512-byte sectors to process
  795. * @nerrors: how many errors to correct per sector (4 or 8)
  796. *
  797. * This function must be executed before any call to gpmc_enable_hwecc_bch.
  798. */
  799. int gpmc_init_hwecc_bch(int cs, int nsectors, int nerrors)
  800. {
  801. /* check if ecc module is in use */
  802. if (gpmc_ecc_used != -EINVAL)
  803. return -EINVAL;
  804. /* support only OMAP3 class */
  805. if (!cpu_is_omap34xx()) {
  806. printk(KERN_ERR "BCH ecc is not supported on this CPU\n");
  807. return -EINVAL;
  808. }
  809. /*
  810. * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1.
  811. * Other chips may be added if confirmed to work.
  812. */
  813. if ((nerrors == 4) &&
  814. (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) {
  815. printk(KERN_ERR "BCH 4-bit mode is not supported on this CPU\n");
  816. return -EINVAL;
  817. }
  818. /* sanity check */
  819. if (nsectors > 8) {
  820. printk(KERN_ERR "BCH cannot process %d sectors (max is 8)\n",
  821. nsectors);
  822. return -EINVAL;
  823. }
  824. return 0;
  825. }
  826. EXPORT_SYMBOL_GPL(gpmc_init_hwecc_bch);
  827. /**
  828. * gpmc_enable_hwecc_bch - enable hardware BCH ecc functionality
  829. * @cs: chip select number
  830. * @mode: read/write mode
  831. * @dev_width: device bus width(1 for x16, 0 for x8)
  832. * @nsectors: how many 512-byte sectors to process
  833. * @nerrors: how many errors to correct per sector (4 or 8)
  834. */
  835. int gpmc_enable_hwecc_bch(int cs, int mode, int dev_width, int nsectors,
  836. int nerrors)
  837. {
  838. unsigned int val;
  839. /* check if ecc module is in use */
  840. if (gpmc_ecc_used != -EINVAL)
  841. return -EINVAL;
  842. gpmc_ecc_used = cs;
  843. /* clear ecc and enable bits */
  844. gpmc_write_reg(GPMC_ECC_CONTROL, 0x1);
  845. /*
  846. * When using BCH, sector size is hardcoded to 512 bytes.
  847. * Here we are using wrapping mode 6 both for reading and writing, with:
  848. * size0 = 0 (no additional protected byte in spare area)
  849. * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
  850. */
  851. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, (32 << 22) | (0 << 12));
  852. /* BCH configuration */
  853. val = ((1 << 16) | /* enable BCH */
  854. (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
  855. (0x06 << 8) | /* wrap mode = 6 */
  856. (dev_width << 7) | /* bus width */
  857. (((nsectors-1) & 0x7) << 4) | /* number of sectors */
  858. (cs << 1) | /* ECC CS */
  859. (0x1)); /* enable ECC */
  860. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  861. gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
  862. return 0;
  863. }
  864. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc_bch);
  865. /**
  866. * gpmc_calculate_ecc_bch4 - Generate 7 ecc bytes per sector of 512 data bytes
  867. * @cs: chip select number
  868. * @dat: The pointer to data on which ecc is computed
  869. * @ecc: The ecc output buffer
  870. */
  871. int gpmc_calculate_ecc_bch4(int cs, const u_char *dat, u_char *ecc)
  872. {
  873. int i;
  874. unsigned long nsectors, reg, val1, val2;
  875. if (gpmc_ecc_used != cs)
  876. return -EINVAL;
  877. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  878. for (i = 0; i < nsectors; i++) {
  879. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  880. /* Read hw-computed remainder */
  881. val1 = gpmc_read_reg(reg + 0);
  882. val2 = gpmc_read_reg(reg + 4);
  883. /*
  884. * Add constant polynomial to remainder, in order to get an ecc
  885. * sequence of 0xFFs for a buffer filled with 0xFFs; and
  886. * left-justify the resulting polynomial.
  887. */
  888. *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF);
  889. *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF);
  890. *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
  891. *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF);
  892. *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF);
  893. *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF);
  894. *ecc++ = 0x7f ^ ((val1 & 0xF) << 4);
  895. }
  896. gpmc_ecc_used = -EINVAL;
  897. return 0;
  898. }
  899. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch4);
  900. /**
  901. * gpmc_calculate_ecc_bch8 - Generate 13 ecc bytes per block of 512 data bytes
  902. * @cs: chip select number
  903. * @dat: The pointer to data on which ecc is computed
  904. * @ecc: The ecc output buffer
  905. */
  906. int gpmc_calculate_ecc_bch8(int cs, const u_char *dat, u_char *ecc)
  907. {
  908. int i;
  909. unsigned long nsectors, reg, val1, val2, val3, val4;
  910. if (gpmc_ecc_used != cs)
  911. return -EINVAL;
  912. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  913. for (i = 0; i < nsectors; i++) {
  914. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  915. /* Read hw-computed remainder */
  916. val1 = gpmc_read_reg(reg + 0);
  917. val2 = gpmc_read_reg(reg + 4);
  918. val3 = gpmc_read_reg(reg + 8);
  919. val4 = gpmc_read_reg(reg + 12);
  920. /*
  921. * Add constant polynomial to remainder, in order to get an ecc
  922. * sequence of 0xFFs for a buffer filled with 0xFFs.
  923. */
  924. *ecc++ = 0xef ^ (val4 & 0xFF);
  925. *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
  926. *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
  927. *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
  928. *ecc++ = 0xed ^ (val3 & 0xFF);
  929. *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
  930. *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
  931. *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
  932. *ecc++ = 0x97 ^ (val2 & 0xFF);
  933. *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
  934. *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
  935. *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
  936. *ecc++ = 0xb5 ^ (val1 & 0xFF);
  937. }
  938. gpmc_ecc_used = -EINVAL;
  939. return 0;
  940. }
  941. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch8);
  942. #endif /* CONFIG_ARCH_OMAP3 */