gpmc.c 33 KB

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