gpmc.c 33 KB

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