docg3.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. /*
  2. * Handles the M-Systems DiskOnChip G3 chip
  3. *
  4. * Copyright (C) 2011 Robert Jarzmik
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/string.h>
  26. #include <linux/slab.h>
  27. #include <linux/io.h>
  28. #include <linux/delay.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/seq_file.h>
  33. #define CREATE_TRACE_POINTS
  34. #include "docg3.h"
  35. /*
  36. * This driver handles the DiskOnChip G3 flash memory.
  37. *
  38. * As no specification is available from M-Systems/Sandisk, this drivers lacks
  39. * several functions available on the chip, as :
  40. * - block erase
  41. * - page write
  42. * - IPL write
  43. * - ECC fixing (lack of BCH algorith understanding)
  44. * - powerdown / powerup
  45. *
  46. * The bus data width (8bits versus 16bits) is not handled (if_cfg flag), and
  47. * the driver assumes a 16bits data bus.
  48. *
  49. * DocG3 relies on 2 ECC algorithms, which are handled in hardware :
  50. * - a 1 byte Hamming code stored in the OOB for each page
  51. * - a 7 bytes BCH code stored in the OOB for each page
  52. * The BCH part is only used for check purpose, no correction is available as
  53. * some information is missing. What is known is that :
  54. * - BCH is in GF(2^14)
  55. * - BCH is over data of 520 bytes (512 page + 7 page_info bytes
  56. * + 1 hamming byte)
  57. * - BCH can correct up to 4 bits (t = 4)
  58. * - BCH syndroms are calculated in hardware, and checked in hardware as well
  59. *
  60. */
  61. static inline u8 doc_readb(struct docg3 *docg3, u16 reg)
  62. {
  63. u8 val = readb(docg3->base + reg);
  64. trace_docg3_io(0, 8, reg, (int)val);
  65. return val;
  66. }
  67. static inline u16 doc_readw(struct docg3 *docg3, u16 reg)
  68. {
  69. u16 val = readw(docg3->base + reg);
  70. trace_docg3_io(0, 16, reg, (int)val);
  71. return val;
  72. }
  73. static inline void doc_writeb(struct docg3 *docg3, u8 val, u16 reg)
  74. {
  75. writeb(val, docg3->base + reg);
  76. trace_docg3_io(1, 8, reg, val);
  77. }
  78. static inline void doc_writew(struct docg3 *docg3, u16 val, u16 reg)
  79. {
  80. writew(val, docg3->base + reg);
  81. trace_docg3_io(1, 16, reg, val);
  82. }
  83. static inline void doc_flash_command(struct docg3 *docg3, u8 cmd)
  84. {
  85. doc_writeb(docg3, cmd, DOC_FLASHCOMMAND);
  86. }
  87. static inline void doc_flash_sequence(struct docg3 *docg3, u8 seq)
  88. {
  89. doc_writeb(docg3, seq, DOC_FLASHSEQUENCE);
  90. }
  91. static inline void doc_flash_address(struct docg3 *docg3, u8 addr)
  92. {
  93. doc_writeb(docg3, addr, DOC_FLASHADDRESS);
  94. }
  95. static char const *part_probes[] = { "cmdlinepart", "saftlpart", NULL };
  96. static int doc_register_readb(struct docg3 *docg3, int reg)
  97. {
  98. u8 val;
  99. doc_writew(docg3, reg, DOC_READADDRESS);
  100. val = doc_readb(docg3, reg);
  101. doc_vdbg("Read register %04x : %02x\n", reg, val);
  102. return val;
  103. }
  104. static int doc_register_readw(struct docg3 *docg3, int reg)
  105. {
  106. u16 val;
  107. doc_writew(docg3, reg, DOC_READADDRESS);
  108. val = doc_readw(docg3, reg);
  109. doc_vdbg("Read register %04x : %04x\n", reg, val);
  110. return val;
  111. }
  112. /**
  113. * doc_delay - delay docg3 operations
  114. * @docg3: the device
  115. * @nbNOPs: the number of NOPs to issue
  116. *
  117. * As no specification is available, the right timings between chip commands are
  118. * unknown. The only available piece of information are the observed nops on a
  119. * working docg3 chip.
  120. * Therefore, doc_delay relies on a busy loop of NOPs, instead of scheduler
  121. * friendlier msleep() functions or blocking mdelay().
  122. */
  123. static void doc_delay(struct docg3 *docg3, int nbNOPs)
  124. {
  125. int i;
  126. doc_vdbg("NOP x %d\n", nbNOPs);
  127. for (i = 0; i < nbNOPs; i++)
  128. doc_writeb(docg3, 0, DOC_NOP);
  129. }
  130. static int is_prot_seq_error(struct docg3 *docg3)
  131. {
  132. int ctrl;
  133. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  134. return ctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR);
  135. }
  136. static int doc_is_ready(struct docg3 *docg3)
  137. {
  138. int ctrl;
  139. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  140. return ctrl & DOC_CTRL_FLASHREADY;
  141. }
  142. static int doc_wait_ready(struct docg3 *docg3)
  143. {
  144. int maxWaitCycles = 100;
  145. do {
  146. doc_delay(docg3, 4);
  147. cpu_relax();
  148. } while (!doc_is_ready(docg3) && maxWaitCycles--);
  149. doc_delay(docg3, 2);
  150. if (maxWaitCycles > 0)
  151. return 0;
  152. else
  153. return -EIO;
  154. }
  155. static int doc_reset_seq(struct docg3 *docg3)
  156. {
  157. int ret;
  158. doc_writeb(docg3, 0x10, DOC_FLASHCONTROL);
  159. doc_flash_sequence(docg3, DOC_SEQ_RESET);
  160. doc_flash_command(docg3, DOC_CMD_RESET);
  161. doc_delay(docg3, 2);
  162. ret = doc_wait_ready(docg3);
  163. doc_dbg("doc_reset_seq() -> isReady=%s\n", ret ? "false" : "true");
  164. return ret;
  165. }
  166. /**
  167. * doc_read_data_area - Read data from data area
  168. * @docg3: the device
  169. * @buf: the buffer to fill in (might be NULL is dummy reads)
  170. * @len: the length to read
  171. * @first: first time read, DOC_READADDRESS should be set
  172. *
  173. * Reads bytes from flash data. Handles the single byte / even bytes reads.
  174. */
  175. static void doc_read_data_area(struct docg3 *docg3, void *buf, int len,
  176. int first)
  177. {
  178. int i, cdr, len4;
  179. u16 data16, *dst16;
  180. u8 data8, *dst8;
  181. doc_dbg("doc_read_data_area(buf=%p, len=%d)\n", buf, len);
  182. cdr = len & 0x3;
  183. len4 = len - cdr;
  184. if (first)
  185. doc_writew(docg3, DOC_IOSPACE_DATA, DOC_READADDRESS);
  186. dst16 = buf;
  187. for (i = 0; i < len4; i += 2) {
  188. data16 = doc_readw(docg3, DOC_IOSPACE_DATA);
  189. if (dst16) {
  190. *dst16 = data16;
  191. dst16++;
  192. }
  193. }
  194. if (cdr) {
  195. doc_writew(docg3, DOC_IOSPACE_DATA | DOC_READADDR_ONE_BYTE,
  196. DOC_READADDRESS);
  197. doc_delay(docg3, 1);
  198. dst8 = (u8 *)dst16;
  199. for (i = 0; i < cdr; i++) {
  200. data8 = doc_readb(docg3, DOC_IOSPACE_DATA);
  201. if (dst8) {
  202. *dst8 = data8;
  203. dst8++;
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * doc_set_data_mode - Sets the flash to reliable data mode
  210. * @docg3: the device
  211. *
  212. * The reliable data mode is a bit slower than the fast mode, but less errors
  213. * occur. Entering the reliable mode cannot be done without entering the fast
  214. * mode first.
  215. */
  216. static void doc_set_reliable_mode(struct docg3 *docg3)
  217. {
  218. doc_dbg("doc_set_reliable_mode()\n");
  219. doc_flash_sequence(docg3, DOC_SEQ_SET_MODE);
  220. doc_flash_command(docg3, DOC_CMD_FAST_MODE);
  221. doc_flash_command(docg3, DOC_CMD_RELIABLE_MODE);
  222. doc_delay(docg3, 2);
  223. }
  224. /**
  225. * doc_set_asic_mode - Set the ASIC mode
  226. * @docg3: the device
  227. * @mode: the mode
  228. *
  229. * The ASIC can work in 3 modes :
  230. * - RESET: all registers are zeroed
  231. * - NORMAL: receives and handles commands
  232. * - POWERDOWN: minimal poweruse, flash parts shut off
  233. */
  234. static void doc_set_asic_mode(struct docg3 *docg3, u8 mode)
  235. {
  236. int i;
  237. for (i = 0; i < 12; i++)
  238. doc_readb(docg3, DOC_IOSPACE_IPL);
  239. mode |= DOC_ASICMODE_MDWREN;
  240. doc_dbg("doc_set_asic_mode(%02x)\n", mode);
  241. doc_writeb(docg3, mode, DOC_ASICMODE);
  242. doc_writeb(docg3, ~mode, DOC_ASICMODECONFIRM);
  243. doc_delay(docg3, 1);
  244. }
  245. /**
  246. * doc_set_device_id - Sets the devices id for cascaded G3 chips
  247. * @docg3: the device
  248. * @id: the chip to select (amongst 0, 1, 2, 3)
  249. *
  250. * There can be 4 cascaded G3 chips. This function selects the one which will
  251. * should be the active one.
  252. */
  253. static void doc_set_device_id(struct docg3 *docg3, int id)
  254. {
  255. u8 ctrl;
  256. doc_dbg("doc_set_device_id(%d)\n", id);
  257. doc_writeb(docg3, id, DOC_DEVICESELECT);
  258. ctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  259. ctrl &= ~DOC_CTRL_VIOLATION;
  260. ctrl |= DOC_CTRL_CE;
  261. doc_writeb(docg3, ctrl, DOC_FLASHCONTROL);
  262. }
  263. /**
  264. * doc_set_extra_page_mode - Change flash page layout
  265. * @docg3: the device
  266. *
  267. * Normally, the flash page is split into the data (512 bytes) and the out of
  268. * band data (16 bytes). For each, 4 more bytes can be accessed, where the wear
  269. * leveling counters are stored. To access this last area of 4 bytes, a special
  270. * mode must be input to the flash ASIC.
  271. *
  272. * Returns 0 if no error occured, -EIO else.
  273. */
  274. static int doc_set_extra_page_mode(struct docg3 *docg3)
  275. {
  276. int fctrl;
  277. doc_dbg("doc_set_extra_page_mode()\n");
  278. doc_flash_sequence(docg3, DOC_SEQ_PAGE_SIZE_532);
  279. doc_flash_command(docg3, DOC_CMD_PAGE_SIZE_532);
  280. doc_delay(docg3, 2);
  281. fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  282. if (fctrl & (DOC_CTRL_PROTECTION_ERROR | DOC_CTRL_SEQUENCE_ERROR))
  283. return -EIO;
  284. else
  285. return 0;
  286. }
  287. /**
  288. * doc_seek - Set both flash planes to the specified block, page for reading
  289. * @docg3: the device
  290. * @block0: the first plane block index
  291. * @block1: the second plane block index
  292. * @page: the page index within the block
  293. * @wear: if true, read will occur on the 4 extra bytes of the wear area
  294. * @ofs: offset in page to read
  295. *
  296. * Programs the flash even and odd planes to the specific block and page.
  297. * Alternatively, programs the flash to the wear area of the specified page.
  298. */
  299. static int doc_read_seek(struct docg3 *docg3, int block0, int block1, int page,
  300. int wear, int ofs)
  301. {
  302. int sector, ret = 0;
  303. doc_dbg("doc_seek(blocks=(%d,%d), page=%d, ofs=%d, wear=%d)\n",
  304. block0, block1, page, ofs, wear);
  305. if (!wear && (ofs < 2 * DOC_LAYOUT_PAGE_SIZE)) {
  306. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE1);
  307. doc_flash_command(docg3, DOC_CMD_READ_PLANE1);
  308. doc_delay(docg3, 2);
  309. } else {
  310. doc_flash_sequence(docg3, DOC_SEQ_SET_PLANE2);
  311. doc_flash_command(docg3, DOC_CMD_READ_PLANE2);
  312. doc_delay(docg3, 2);
  313. }
  314. doc_set_reliable_mode(docg3);
  315. if (wear)
  316. ret = doc_set_extra_page_mode(docg3);
  317. if (ret)
  318. goto out;
  319. sector = (block0 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  320. doc_flash_sequence(docg3, DOC_SEQ_READ);
  321. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  322. doc_delay(docg3, 1);
  323. doc_flash_address(docg3, sector & 0xff);
  324. doc_flash_address(docg3, (sector >> 8) & 0xff);
  325. doc_flash_address(docg3, (sector >> 16) & 0xff);
  326. doc_delay(docg3, 1);
  327. sector = (block1 << DOC_ADDR_BLOCK_SHIFT) + (page & DOC_ADDR_PAGE_MASK);
  328. doc_flash_command(docg3, DOC_CMD_PROG_BLOCK_ADDR);
  329. doc_delay(docg3, 1);
  330. doc_flash_address(docg3, sector & 0xff);
  331. doc_flash_address(docg3, (sector >> 8) & 0xff);
  332. doc_flash_address(docg3, (sector >> 16) & 0xff);
  333. doc_delay(docg3, 2);
  334. out:
  335. return ret;
  336. }
  337. /**
  338. * doc_read_page_ecc_init - Initialize hardware ECC engine
  339. * @docg3: the device
  340. * @len: the number of bytes covered by the ECC (BCH covered)
  341. *
  342. * The function does initialize the hardware ECC engine to compute the Hamming
  343. * ECC (on 1 byte) and the BCH Syndroms (on 7 bytes).
  344. *
  345. * Return 0 if succeeded, -EIO on error
  346. */
  347. static int doc_read_page_ecc_init(struct docg3 *docg3, int len)
  348. {
  349. doc_writew(docg3, DOC_ECCCONF0_READ_MODE
  350. | DOC_ECCCONF0_BCH_ENABLE | DOC_ECCCONF0_HAMMING_ENABLE
  351. | (len & DOC_ECCCONF0_DATA_BYTES_MASK),
  352. DOC_ECCCONF0);
  353. doc_delay(docg3, 4);
  354. doc_register_readb(docg3, DOC_FLASHCONTROL);
  355. return doc_wait_ready(docg3);
  356. }
  357. /**
  358. * doc_read_page_prepare - Prepares reading data from a flash page
  359. * @docg3: the device
  360. * @block0: the first plane block index on flash memory
  361. * @block1: the second plane block index on flash memory
  362. * @page: the page index in the block
  363. * @offset: the offset in the page (must be a multiple of 4)
  364. *
  365. * Prepares the page to be read in the flash memory :
  366. * - tell ASIC to map the flash pages
  367. * - tell ASIC to be in read mode
  368. *
  369. * After a call to this method, a call to doc_read_page_finish is mandatory,
  370. * to end the read cycle of the flash.
  371. *
  372. * Read data from a flash page. The length to be read must be between 0 and
  373. * (page_size + oob_size + wear_size), ie. 532, and a multiple of 4 (because
  374. * the extra bytes reading is not implemented).
  375. *
  376. * As pages are grouped by 2 (in 2 planes), reading from a page must be done
  377. * in two steps:
  378. * - one read of 512 bytes at offset 0
  379. * - one read of 512 bytes at offset 512 + 16
  380. *
  381. * Returns 0 if successful, -EIO if a read error occured.
  382. */
  383. static int doc_read_page_prepare(struct docg3 *docg3, int block0, int block1,
  384. int page, int offset)
  385. {
  386. int wear_area = 0, ret = 0;
  387. doc_dbg("doc_read_page_prepare(blocks=(%d,%d), page=%d, ofsInPage=%d)\n",
  388. block0, block1, page, offset);
  389. if (offset >= DOC_LAYOUT_WEAR_OFFSET)
  390. wear_area = 1;
  391. if (!wear_area && offset > (DOC_LAYOUT_PAGE_OOB_SIZE * 2))
  392. return -EINVAL;
  393. doc_set_device_id(docg3, docg3->device_id);
  394. ret = doc_reset_seq(docg3);
  395. if (ret)
  396. goto err;
  397. /* Program the flash address block and page */
  398. ret = doc_read_seek(docg3, block0, block1, page, wear_area, offset);
  399. if (ret)
  400. goto err;
  401. doc_flash_command(docg3, DOC_CMD_READ_ALL_PLANES);
  402. doc_delay(docg3, 2);
  403. doc_wait_ready(docg3);
  404. doc_flash_command(docg3, DOC_CMD_SET_ADDR_READ);
  405. doc_delay(docg3, 1);
  406. if (offset >= DOC_LAYOUT_PAGE_SIZE * 2)
  407. offset -= 2 * DOC_LAYOUT_PAGE_SIZE;
  408. doc_flash_address(docg3, offset >> 2);
  409. doc_delay(docg3, 1);
  410. doc_wait_ready(docg3);
  411. doc_flash_command(docg3, DOC_CMD_READ_FLASH);
  412. return 0;
  413. err:
  414. doc_writeb(docg3, 0, DOC_DATAEND);
  415. doc_delay(docg3, 2);
  416. return -EIO;
  417. }
  418. /**
  419. * doc_read_page_getbytes - Reads bytes from a prepared page
  420. * @docg3: the device
  421. * @len: the number of bytes to be read (must be a multiple of 4)
  422. * @buf: the buffer to be filled in
  423. * @first: 1 if first time read, DOC_READADDRESS should be set
  424. *
  425. */
  426. static int doc_read_page_getbytes(struct docg3 *docg3, int len, u_char *buf,
  427. int first)
  428. {
  429. doc_read_data_area(docg3, buf, len, first);
  430. doc_delay(docg3, 2);
  431. return len;
  432. }
  433. /**
  434. * doc_get_hw_bch_syndroms - Get hardware calculated BCH syndroms
  435. * @docg3: the device
  436. * @syns: the array of 7 integers where the syndroms will be stored
  437. */
  438. static void doc_get_hw_bch_syndroms(struct docg3 *docg3, int *syns)
  439. {
  440. int i;
  441. for (i = 0; i < DOC_ECC_BCH_SIZE; i++)
  442. syns[i] = doc_register_readb(docg3, DOC_BCH_SYNDROM(i));
  443. }
  444. /**
  445. * doc_read_page_finish - Ends reading of a flash page
  446. * @docg3: the device
  447. *
  448. * As a side effect, resets the chip selector to 0. This ensures that after each
  449. * read operation, the floor 0 is selected. Therefore, if the systems halts, the
  450. * reboot will boot on floor 0, where the IPL is.
  451. */
  452. static void doc_read_page_finish(struct docg3 *docg3)
  453. {
  454. doc_writeb(docg3, 0, DOC_DATAEND);
  455. doc_delay(docg3, 2);
  456. doc_set_device_id(docg3, 0);
  457. }
  458. /**
  459. * calc_block_sector - Calculate blocks, pages and ofs.
  460. * @from: offset in flash
  461. * @block0: first plane block index calculated
  462. * @block1: second plane block index calculated
  463. * @page: page calculated
  464. * @ofs: offset in page
  465. */
  466. static void calc_block_sector(loff_t from, int *block0, int *block1, int *page,
  467. int *ofs)
  468. {
  469. uint sector;
  470. sector = from / DOC_LAYOUT_PAGE_SIZE;
  471. *block0 = sector / (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES)
  472. * DOC_LAYOUT_NBPLANES;
  473. *block1 = *block0 + 1;
  474. *page = sector % (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_NBPLANES);
  475. *page /= DOC_LAYOUT_NBPLANES;
  476. if (sector % 2)
  477. *ofs = DOC_LAYOUT_PAGE_OOB_SIZE;
  478. else
  479. *ofs = 0;
  480. }
  481. /**
  482. * doc_read_oob - Read out of band bytes from flash
  483. * @mtd: the device
  484. * @from: the offset from first block and first page, in bytes, aligned on page
  485. * size
  486. * @ops: the mtd oob structure
  487. *
  488. * Reads flash memory OOB area of pages.
  489. *
  490. * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
  491. */
  492. static int doc_read_oob(struct mtd_info *mtd, loff_t from,
  493. struct mtd_oob_ops *ops)
  494. {
  495. struct docg3 *docg3 = mtd->priv;
  496. int block0, block1, page, ret, ofs = 0;
  497. u8 *oobbuf = ops->oobbuf;
  498. u8 *buf = ops->datbuf;
  499. size_t len, ooblen, nbdata, nboob;
  500. u8 calc_ecc[DOC_ECC_BCH_SIZE], eccconf1;
  501. if (buf)
  502. len = ops->len;
  503. else
  504. len = 0;
  505. if (oobbuf)
  506. ooblen = ops->ooblen;
  507. else
  508. ooblen = 0;
  509. if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB)
  510. oobbuf += ops->ooboffs;
  511. doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n",
  512. from, ops->mode, buf, len, oobbuf, ooblen);
  513. if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % DOC_LAYOUT_OOB_SIZE) ||
  514. (from % DOC_LAYOUT_PAGE_SIZE))
  515. return -EINVAL;
  516. ret = -EINVAL;
  517. calc_block_sector(from + len, &block0, &block1, &page, &ofs);
  518. if (block1 > docg3->max_block)
  519. goto err;
  520. ops->oobretlen = 0;
  521. ops->retlen = 0;
  522. ret = 0;
  523. while (!ret && (len > 0 || ooblen > 0)) {
  524. calc_block_sector(from, &block0, &block1, &page, &ofs);
  525. nbdata = min_t(size_t, len, (size_t)DOC_LAYOUT_PAGE_SIZE);
  526. nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE);
  527. ret = doc_read_page_prepare(docg3, block0, block1, page, ofs);
  528. if (ret < 0)
  529. goto err;
  530. ret = doc_read_page_ecc_init(docg3, DOC_ECC_BCH_COVERED_BYTES);
  531. if (ret < 0)
  532. goto err_in_read;
  533. ret = doc_read_page_getbytes(docg3, nbdata, buf, 1);
  534. if (ret < nbdata)
  535. goto err_in_read;
  536. doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata,
  537. NULL, 0);
  538. ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0);
  539. if (ret < nboob)
  540. goto err_in_read;
  541. doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob,
  542. NULL, 0);
  543. doc_get_hw_bch_syndroms(docg3, calc_ecc);
  544. eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1);
  545. if (nboob >= DOC_LAYOUT_OOB_SIZE) {
  546. doc_dbg("OOB - INFO: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  547. oobbuf[0], oobbuf[1], oobbuf[2], oobbuf[3],
  548. oobbuf[4], oobbuf[5], oobbuf[6]);
  549. doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]);
  550. doc_dbg("OOB - BCH_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  551. oobbuf[8], oobbuf[9], oobbuf[10], oobbuf[11],
  552. oobbuf[12], oobbuf[13], oobbuf[14]);
  553. doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]);
  554. }
  555. doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1);
  556. doc_dbg("ECC CALC_ECC: %02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  557. calc_ecc[0], calc_ecc[1], calc_ecc[2],
  558. calc_ecc[3], calc_ecc[4], calc_ecc[5],
  559. calc_ecc[6]);
  560. ret = -EBADMSG;
  561. if (block0 >= DOC_LAYOUT_BLOCK_FIRST_DATA) {
  562. if ((eccconf1 & DOC_ECCCONF1_BCH_SYNDROM_ERR) &&
  563. (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN))
  564. goto err_in_read;
  565. if (is_prot_seq_error(docg3))
  566. goto err_in_read;
  567. }
  568. doc_read_page_finish(docg3);
  569. ops->retlen += nbdata;
  570. ops->oobretlen += nboob;
  571. buf += nbdata;
  572. oobbuf += nboob;
  573. len -= nbdata;
  574. ooblen -= nboob;
  575. from += DOC_LAYOUT_PAGE_SIZE;
  576. }
  577. return 0;
  578. err_in_read:
  579. doc_read_page_finish(docg3);
  580. err:
  581. return ret;
  582. }
  583. /**
  584. * doc_read - Read bytes from flash
  585. * @mtd: the device
  586. * @from: the offset from first block and first page, in bytes, aligned on page
  587. * size
  588. * @len: the number of bytes to read (must be a multiple of 4)
  589. * @retlen: the number of bytes actually read
  590. * @buf: the filled in buffer
  591. *
  592. * Reads flash memory pages. This function does not read the OOB chunk, but only
  593. * the page data.
  594. *
  595. * Returns 0 if read successfull, of -EIO, -EINVAL if an error occured
  596. */
  597. static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
  598. size_t *retlen, u_char *buf)
  599. {
  600. struct mtd_oob_ops ops;
  601. size_t ret;
  602. memset(&ops, 0, sizeof(ops));
  603. ops.datbuf = buf;
  604. ops.len = len;
  605. ops.mode = MTD_OPS_AUTO_OOB;
  606. ret = doc_read_oob(mtd, from, &ops);
  607. *retlen = ops.retlen;
  608. return ret;
  609. }
  610. static int doc_reload_bbt(struct docg3 *docg3)
  611. {
  612. int block = DOC_LAYOUT_BLOCK_BBT;
  613. int ret = 0, nbpages, page;
  614. u_char *buf = docg3->bbt;
  615. nbpages = DIV_ROUND_UP(docg3->max_block + 1, 8 * DOC_LAYOUT_PAGE_SIZE);
  616. for (page = 0; !ret && (page < nbpages); page++) {
  617. ret = doc_read_page_prepare(docg3, block, block + 1,
  618. page + DOC_LAYOUT_PAGE_BBT, 0);
  619. if (!ret)
  620. ret = doc_read_page_ecc_init(docg3,
  621. DOC_LAYOUT_PAGE_SIZE);
  622. if (!ret)
  623. doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE,
  624. buf, 1);
  625. buf += DOC_LAYOUT_PAGE_SIZE;
  626. }
  627. doc_read_page_finish(docg3);
  628. return ret;
  629. }
  630. /**
  631. * doc_block_isbad - Checks whether a block is good or not
  632. * @mtd: the device
  633. * @from: the offset to find the correct block
  634. *
  635. * Returns 1 if block is bad, 0 if block is good
  636. */
  637. static int doc_block_isbad(struct mtd_info *mtd, loff_t from)
  638. {
  639. struct docg3 *docg3 = mtd->priv;
  640. int block0, block1, page, ofs, is_good;
  641. calc_block_sector(from, &block0, &block1, &page, &ofs);
  642. doc_dbg("doc_block_isbad(from=%lld) => block=(%d,%d), page=%d, ofs=%d\n",
  643. from, block0, block1, page, ofs);
  644. if (block0 < DOC_LAYOUT_BLOCK_FIRST_DATA)
  645. return 0;
  646. if (block1 > docg3->max_block)
  647. return -EINVAL;
  648. is_good = docg3->bbt[block0 >> 3] & (1 << (block0 & 0x7));
  649. return !is_good;
  650. }
  651. /**
  652. * doc_get_erase_count - Get block erase count
  653. * @docg3: the device
  654. * @from: the offset in which the block is.
  655. *
  656. * Get the number of times a block was erased. The number is the maximum of
  657. * erase times between first and second plane (which should be equal normally).
  658. *
  659. * Returns The number of erases, or -EINVAL or -EIO on error.
  660. */
  661. static int doc_get_erase_count(struct docg3 *docg3, loff_t from)
  662. {
  663. u8 buf[DOC_LAYOUT_WEAR_SIZE];
  664. int ret, plane1_erase_count, plane2_erase_count;
  665. int block0, block1, page, ofs;
  666. doc_dbg("doc_get_erase_count(from=%lld, buf=%p)\n", from, buf);
  667. if (from % DOC_LAYOUT_PAGE_SIZE)
  668. return -EINVAL;
  669. calc_block_sector(from, &block0, &block1, &page, &ofs);
  670. if (block1 > docg3->max_block)
  671. return -EINVAL;
  672. ret = doc_reset_seq(docg3);
  673. if (!ret)
  674. ret = doc_read_page_prepare(docg3, block0, block1, page,
  675. ofs + DOC_LAYOUT_WEAR_OFFSET);
  676. if (!ret)
  677. ret = doc_read_page_getbytes(docg3, DOC_LAYOUT_WEAR_SIZE,
  678. buf, 1);
  679. doc_read_page_finish(docg3);
  680. if (ret || (buf[0] != DOC_ERASE_MARK) || (buf[2] != DOC_ERASE_MARK))
  681. return -EIO;
  682. plane1_erase_count = (u8)(~buf[1]) | ((u8)(~buf[4]) << 8)
  683. | ((u8)(~buf[5]) << 16);
  684. plane2_erase_count = (u8)(~buf[3]) | ((u8)(~buf[6]) << 8)
  685. | ((u8)(~buf[7]) << 16);
  686. return max(plane1_erase_count, plane2_erase_count);
  687. }
  688. /*
  689. * Debug sysfs entries
  690. */
  691. static int dbg_flashctrl_show(struct seq_file *s, void *p)
  692. {
  693. struct docg3 *docg3 = (struct docg3 *)s->private;
  694. int pos = 0;
  695. u8 fctrl = doc_register_readb(docg3, DOC_FLASHCONTROL);
  696. pos += seq_printf(s,
  697. "FlashControl : 0x%02x (%s,CE# %s,%s,%s,flash %s)\n",
  698. fctrl,
  699. fctrl & DOC_CTRL_VIOLATION ? "protocol violation" : "-",
  700. fctrl & DOC_CTRL_CE ? "active" : "inactive",
  701. fctrl & DOC_CTRL_PROTECTION_ERROR ? "protection error" : "-",
  702. fctrl & DOC_CTRL_SEQUENCE_ERROR ? "sequence error" : "-",
  703. fctrl & DOC_CTRL_FLASHREADY ? "ready" : "not ready");
  704. return pos;
  705. }
  706. DEBUGFS_RO_ATTR(flashcontrol, dbg_flashctrl_show);
  707. static int dbg_asicmode_show(struct seq_file *s, void *p)
  708. {
  709. struct docg3 *docg3 = (struct docg3 *)s->private;
  710. int pos = 0;
  711. int pctrl = doc_register_readb(docg3, DOC_ASICMODE);
  712. int mode = pctrl & 0x03;
  713. pos += seq_printf(s,
  714. "%04x : RAM_WE=%d,RSTIN_RESET=%d,BDETCT_RESET=%d,WRITE_ENABLE=%d,POWERDOWN=%d,MODE=%d%d (",
  715. pctrl,
  716. pctrl & DOC_ASICMODE_RAM_WE ? 1 : 0,
  717. pctrl & DOC_ASICMODE_RSTIN_RESET ? 1 : 0,
  718. pctrl & DOC_ASICMODE_BDETCT_RESET ? 1 : 0,
  719. pctrl & DOC_ASICMODE_MDWREN ? 1 : 0,
  720. pctrl & DOC_ASICMODE_POWERDOWN ? 1 : 0,
  721. mode >> 1, mode & 0x1);
  722. switch (mode) {
  723. case DOC_ASICMODE_RESET:
  724. pos += seq_printf(s, "reset");
  725. break;
  726. case DOC_ASICMODE_NORMAL:
  727. pos += seq_printf(s, "normal");
  728. break;
  729. case DOC_ASICMODE_POWERDOWN:
  730. pos += seq_printf(s, "powerdown");
  731. break;
  732. }
  733. pos += seq_printf(s, ")\n");
  734. return pos;
  735. }
  736. DEBUGFS_RO_ATTR(asic_mode, dbg_asicmode_show);
  737. static int dbg_device_id_show(struct seq_file *s, void *p)
  738. {
  739. struct docg3 *docg3 = (struct docg3 *)s->private;
  740. int pos = 0;
  741. int id = doc_register_readb(docg3, DOC_DEVICESELECT);
  742. pos += seq_printf(s, "DeviceId = %d\n", id);
  743. return pos;
  744. }
  745. DEBUGFS_RO_ATTR(device_id, dbg_device_id_show);
  746. static int dbg_protection_show(struct seq_file *s, void *p)
  747. {
  748. struct docg3 *docg3 = (struct docg3 *)s->private;
  749. int pos = 0;
  750. int protect, dps0, dps0_low, dps0_high, dps1, dps1_low, dps1_high;
  751. protect = doc_register_readb(docg3, DOC_PROTECTION);
  752. dps0 = doc_register_readb(docg3, DOC_DPS0_STATUS);
  753. dps0_low = doc_register_readw(docg3, DOC_DPS0_ADDRLOW);
  754. dps0_high = doc_register_readw(docg3, DOC_DPS0_ADDRHIGH);
  755. dps1 = doc_register_readb(docg3, DOC_DPS1_STATUS);
  756. dps1_low = doc_register_readw(docg3, DOC_DPS1_ADDRLOW);
  757. dps1_high = doc_register_readw(docg3, DOC_DPS1_ADDRHIGH);
  758. pos += seq_printf(s, "Protection = 0x%02x (",
  759. protect);
  760. if (protect & DOC_PROTECT_FOUNDRY_OTP_LOCK)
  761. pos += seq_printf(s, "FOUNDRY_OTP_LOCK,");
  762. if (protect & DOC_PROTECT_CUSTOMER_OTP_LOCK)
  763. pos += seq_printf(s, "CUSTOMER_OTP_LOCK,");
  764. if (protect & DOC_PROTECT_LOCK_INPUT)
  765. pos += seq_printf(s, "LOCK_INPUT,");
  766. if (protect & DOC_PROTECT_STICKY_LOCK)
  767. pos += seq_printf(s, "STICKY_LOCK,");
  768. if (protect & DOC_PROTECT_PROTECTION_ENABLED)
  769. pos += seq_printf(s, "PROTECTION ON,");
  770. if (protect & DOC_PROTECT_IPL_DOWNLOAD_LOCK)
  771. pos += seq_printf(s, "IPL_DOWNLOAD_LOCK,");
  772. if (protect & DOC_PROTECT_PROTECTION_ERROR)
  773. pos += seq_printf(s, "PROTECT_ERR,");
  774. else
  775. pos += seq_printf(s, "NO_PROTECT_ERR");
  776. pos += seq_printf(s, ")\n");
  777. pos += seq_printf(s, "DPS0 = 0x%02x : "
  778. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  779. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  780. dps0, dps0_low, dps0_high,
  781. !!(dps0 & DOC_DPS_OTP_PROTECTED),
  782. !!(dps0 & DOC_DPS_READ_PROTECTED),
  783. !!(dps0 & DOC_DPS_WRITE_PROTECTED),
  784. !!(dps0 & DOC_DPS_HW_LOCK_ENABLED),
  785. !!(dps0 & DOC_DPS_KEY_OK));
  786. pos += seq_printf(s, "DPS1 = 0x%02x : "
  787. "Protected area [0x%x - 0x%x] : OTP=%d, READ=%d, "
  788. "WRITE=%d, HW_LOCK=%d, KEY_OK=%d\n",
  789. dps1, dps1_low, dps1_high,
  790. !!(dps1 & DOC_DPS_OTP_PROTECTED),
  791. !!(dps1 & DOC_DPS_READ_PROTECTED),
  792. !!(dps1 & DOC_DPS_WRITE_PROTECTED),
  793. !!(dps1 & DOC_DPS_HW_LOCK_ENABLED),
  794. !!(dps1 & DOC_DPS_KEY_OK));
  795. return pos;
  796. }
  797. DEBUGFS_RO_ATTR(protection, dbg_protection_show);
  798. static int __init doc_dbg_register(struct docg3 *docg3)
  799. {
  800. struct dentry *root, *entry;
  801. root = debugfs_create_dir("docg3", NULL);
  802. if (!root)
  803. return -ENOMEM;
  804. entry = debugfs_create_file("flashcontrol", S_IRUSR, root, docg3,
  805. &flashcontrol_fops);
  806. if (entry)
  807. entry = debugfs_create_file("asic_mode", S_IRUSR, root,
  808. docg3, &asic_mode_fops);
  809. if (entry)
  810. entry = debugfs_create_file("device_id", S_IRUSR, root,
  811. docg3, &device_id_fops);
  812. if (entry)
  813. entry = debugfs_create_file("protection", S_IRUSR, root,
  814. docg3, &protection_fops);
  815. if (entry) {
  816. docg3->debugfs_root = root;
  817. return 0;
  818. } else {
  819. debugfs_remove_recursive(root);
  820. return -ENOMEM;
  821. }
  822. }
  823. static void __exit doc_dbg_unregister(struct docg3 *docg3)
  824. {
  825. debugfs_remove_recursive(docg3->debugfs_root);
  826. }
  827. /**
  828. * doc_set_driver_info - Fill the mtd_info structure and docg3 structure
  829. * @chip_id: The chip ID of the supported chip
  830. * @mtd: The structure to fill
  831. */
  832. static void __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
  833. {
  834. struct docg3 *docg3 = mtd->priv;
  835. int cfg;
  836. cfg = doc_register_readb(docg3, DOC_CONFIGURATION);
  837. docg3->if_cfg = (cfg & DOC_CONF_IF_CFG ? 1 : 0);
  838. switch (chip_id) {
  839. case DOC_CHIPID_G3:
  840. mtd->name = kasprintf(GFP_KERNEL, "DiskOnChip G3 floor %d",
  841. docg3->device_id);
  842. docg3->max_block = 2047;
  843. break;
  844. }
  845. mtd->type = MTD_NANDFLASH;
  846. /*
  847. * Once write methods are added, the correct flags will be set.
  848. * mtd->flags = MTD_CAP_NANDFLASH;
  849. */
  850. mtd->flags = MTD_CAP_ROM;
  851. mtd->size = (docg3->max_block + 1) * DOC_LAYOUT_BLOCK_SIZE;
  852. mtd->erasesize = DOC_LAYOUT_BLOCK_SIZE * DOC_LAYOUT_NBPLANES;
  853. mtd->writesize = DOC_LAYOUT_PAGE_SIZE;
  854. mtd->oobsize = DOC_LAYOUT_OOB_SIZE;
  855. mtd->owner = THIS_MODULE;
  856. mtd->erase = NULL;
  857. mtd->point = NULL;
  858. mtd->unpoint = NULL;
  859. mtd->read = doc_read;
  860. mtd->write = NULL;
  861. mtd->read_oob = doc_read_oob;
  862. mtd->write_oob = NULL;
  863. mtd->sync = NULL;
  864. mtd->block_isbad = doc_block_isbad;
  865. }
  866. /**
  867. * doc_probe_device - Check if a device is available
  868. * @base: the io space where the device is probed
  869. * @floor: the floor of the probed device
  870. * @dev: the device
  871. *
  872. * Checks whether a device at the specified IO range, and floor is available.
  873. *
  874. * Returns a mtd_info struct if there is a device, ENODEV if none found, ENOMEM
  875. * if a memory allocation failed. If floor 0 is checked, a reset of the ASIC is
  876. * launched.
  877. */
  878. static struct mtd_info *doc_probe_device(void __iomem *base, int floor,
  879. struct device *dev)
  880. {
  881. int ret, bbt_nbpages;
  882. u16 chip_id, chip_id_inv;
  883. struct docg3 *docg3;
  884. struct mtd_info *mtd;
  885. ret = -ENOMEM;
  886. docg3 = kzalloc(sizeof(struct docg3), GFP_KERNEL);
  887. if (!docg3)
  888. goto nomem1;
  889. mtd = kzalloc(sizeof(struct mtd_info), GFP_KERNEL);
  890. if (!mtd)
  891. goto nomem2;
  892. mtd->priv = docg3;
  893. bbt_nbpages = DIV_ROUND_UP(docg3->max_block + 1,
  894. 8 * DOC_LAYOUT_PAGE_SIZE);
  895. docg3->bbt = kzalloc(bbt_nbpages * DOC_LAYOUT_PAGE_SIZE, GFP_KERNEL);
  896. if (!docg3->bbt)
  897. goto nomem3;
  898. docg3->dev = dev;
  899. docg3->device_id = floor;
  900. docg3->base = base;
  901. doc_set_device_id(docg3, docg3->device_id);
  902. if (!floor)
  903. doc_set_asic_mode(docg3, DOC_ASICMODE_RESET);
  904. doc_set_asic_mode(docg3, DOC_ASICMODE_NORMAL);
  905. chip_id = doc_register_readw(docg3, DOC_CHIPID);
  906. chip_id_inv = doc_register_readw(docg3, DOC_CHIPID_INV);
  907. ret = 0;
  908. if (chip_id != (u16)(~chip_id_inv)) {
  909. goto nomem3;
  910. }
  911. switch (chip_id) {
  912. case DOC_CHIPID_G3:
  913. doc_info("Found a G3 DiskOnChip at addr %p, floor %d\n",
  914. base, floor);
  915. break;
  916. default:
  917. doc_err("Chip id %04x is not a DiskOnChip G3 chip\n", chip_id);
  918. goto nomem3;
  919. }
  920. doc_set_driver_info(chip_id, mtd);
  921. doc_reload_bbt(docg3);
  922. return mtd;
  923. nomem3:
  924. kfree(mtd);
  925. nomem2:
  926. kfree(docg3);
  927. nomem1:
  928. return ERR_PTR(ret);
  929. }
  930. /**
  931. * doc_release_device - Release a docg3 floor
  932. * @mtd: the device
  933. */
  934. static void doc_release_device(struct mtd_info *mtd)
  935. {
  936. struct docg3 *docg3 = mtd->priv;
  937. mtd_device_unregister(mtd);
  938. kfree(docg3->bbt);
  939. kfree(docg3);
  940. kfree(mtd->name);
  941. kfree(mtd);
  942. }
  943. /**
  944. * doc_probe - Probe the IO space for a DiskOnChip G3 chip
  945. * @pdev: platform device
  946. *
  947. * Probes for a G3 chip at the specified IO space in the platform data
  948. * ressources. The floor 0 must be available.
  949. *
  950. * Returns 0 on success, -ENOMEM, -ENXIO on error
  951. */
  952. static int __init docg3_probe(struct platform_device *pdev)
  953. {
  954. struct device *dev = &pdev->dev;
  955. struct mtd_info *mtd;
  956. struct resource *ress;
  957. void __iomem *base;
  958. int ret, floor, found = 0;
  959. struct mtd_info **docg3_floors;
  960. ret = -ENXIO;
  961. ress = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  962. if (!ress) {
  963. dev_err(dev, "No I/O memory resource defined\n");
  964. goto noress;
  965. }
  966. base = ioremap(ress->start, DOC_IOSPACE_SIZE);
  967. ret = -ENOMEM;
  968. docg3_floors = kzalloc(sizeof(*docg3_floors) * DOC_MAX_NBFLOORS,
  969. GFP_KERNEL);
  970. if (!docg3_floors)
  971. goto nomem;
  972. ret = 0;
  973. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++) {
  974. mtd = doc_probe_device(base, floor, dev);
  975. if (floor == 0 && !mtd)
  976. goto notfound;
  977. if (!IS_ERR_OR_NULL(mtd))
  978. ret = mtd_device_parse_register(mtd, part_probes,
  979. NULL, NULL, 0);
  980. else
  981. ret = PTR_ERR(mtd);
  982. docg3_floors[floor] = mtd;
  983. if (ret)
  984. goto err_probe;
  985. if (mtd)
  986. found++;
  987. }
  988. if (!found)
  989. goto notfound;
  990. platform_set_drvdata(pdev, docg3_floors);
  991. doc_dbg_register(docg3_floors[0]->priv);
  992. return 0;
  993. notfound:
  994. ret = -ENODEV;
  995. dev_info(dev, "No supported DiskOnChip found\n");
  996. err_probe:
  997. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  998. if (docg3_floors[floor])
  999. doc_release_device(docg3_floors[floor]);
  1000. nomem:
  1001. iounmap(base);
  1002. noress:
  1003. return ret;
  1004. }
  1005. /**
  1006. * docg3_release - Release the driver
  1007. * @pdev: the platform device
  1008. *
  1009. * Returns 0
  1010. */
  1011. static int __exit docg3_release(struct platform_device *pdev)
  1012. {
  1013. struct mtd_info **docg3_floors = platform_get_drvdata(pdev);
  1014. struct docg3 *docg3 = docg3_floors[0]->priv;
  1015. void __iomem *base = docg3->base;
  1016. int floor;
  1017. doc_dbg_unregister(docg3);
  1018. for (floor = 0; floor < DOC_MAX_NBFLOORS; floor++)
  1019. if (docg3_floors[floor])
  1020. doc_release_device(docg3_floors[floor]);
  1021. kfree(docg3_floors);
  1022. iounmap(base);
  1023. return 0;
  1024. }
  1025. static struct platform_driver g3_driver = {
  1026. .driver = {
  1027. .name = "docg3",
  1028. .owner = THIS_MODULE,
  1029. },
  1030. .remove = __exit_p(docg3_release),
  1031. };
  1032. static int __init docg3_init(void)
  1033. {
  1034. return platform_driver_probe(&g3_driver, docg3_probe);
  1035. }
  1036. module_init(docg3_init);
  1037. static void __exit docg3_exit(void)
  1038. {
  1039. platform_driver_unregister(&g3_driver);
  1040. }
  1041. module_exit(docg3_exit);
  1042. MODULE_LICENSE("GPL");
  1043. MODULE_AUTHOR("Robert Jarzmik <robert.jarzmik@free.fr>");
  1044. MODULE_DESCRIPTION("MTD driver for DiskOnChip G3");