aic94xx_sds.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Aic94xx SAS/SATA driver access to shared data structures and memory
  4. * maps.
  5. *
  6. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  7. * Copyright (C) 2005 Luben Tuikov <[email protected]>
  8. */
  9. #include <linux/pci.h>
  10. #include <linux/slab.h>
  11. #include <linux/delay.h>
  12. #include "aic94xx.h"
  13. #include "aic94xx_reg.h"
  14. #include "aic94xx_sds.h"
  15. /* ---------- OCM stuff ---------- */
  16. struct asd_ocm_dir_ent {
  17. u8 type;
  18. u8 offs[3];
  19. u8 _r1;
  20. u8 size[3];
  21. } __attribute__ ((packed));
  22. struct asd_ocm_dir {
  23. char sig[2];
  24. u8 _r1[2];
  25. u8 major; /* 0 */
  26. u8 minor; /* 0 */
  27. u8 _r2;
  28. u8 num_de;
  29. struct asd_ocm_dir_ent entry[15];
  30. } __attribute__ ((packed));
  31. #define OCM_DE_OCM_DIR 0x00
  32. #define OCM_DE_WIN_DRVR 0x01
  33. #define OCM_DE_BIOS_CHIM 0x02
  34. #define OCM_DE_RAID_ENGN 0x03
  35. #define OCM_DE_BIOS_INTL 0x04
  36. #define OCM_DE_BIOS_CHIM_OSM 0x05
  37. #define OCM_DE_BIOS_CHIM_DYNAMIC 0x06
  38. #define OCM_DE_ADDC2C_RES0 0x07
  39. #define OCM_DE_ADDC2C_RES1 0x08
  40. #define OCM_DE_ADDC2C_RES2 0x09
  41. #define OCM_DE_ADDC2C_RES3 0x0A
  42. #define OCM_INIT_DIR_ENTRIES 5
  43. /***************************************************************************
  44. * OCM directory default
  45. ***************************************************************************/
  46. static struct asd_ocm_dir OCMDirInit =
  47. {
  48. .sig = {0x4D, 0x4F}, /* signature */
  49. .num_de = OCM_INIT_DIR_ENTRIES, /* no. of directory entries */
  50. };
  51. /***************************************************************************
  52. * OCM directory Entries default
  53. ***************************************************************************/
  54. static struct asd_ocm_dir_ent OCMDirEntriesInit[OCM_INIT_DIR_ENTRIES] =
  55. {
  56. {
  57. .type = (OCM_DE_ADDC2C_RES0), /* Entry type */
  58. .offs = {128}, /* Offset */
  59. .size = {0, 4}, /* size */
  60. },
  61. {
  62. .type = (OCM_DE_ADDC2C_RES1), /* Entry type */
  63. .offs = {128, 4}, /* Offset */
  64. .size = {0, 4}, /* size */
  65. },
  66. {
  67. .type = (OCM_DE_ADDC2C_RES2), /* Entry type */
  68. .offs = {128, 8}, /* Offset */
  69. .size = {0, 4}, /* size */
  70. },
  71. {
  72. .type = (OCM_DE_ADDC2C_RES3), /* Entry type */
  73. .offs = {128, 12}, /* Offset */
  74. .size = {0, 4}, /* size */
  75. },
  76. {
  77. .type = (OCM_DE_WIN_DRVR), /* Entry type */
  78. .offs = {128, 16}, /* Offset */
  79. .size = {128, 235, 1}, /* size */
  80. },
  81. };
  82. struct asd_bios_chim_struct {
  83. char sig[4];
  84. u8 major; /* 1 */
  85. u8 minor; /* 0 */
  86. u8 bios_major;
  87. u8 bios_minor;
  88. __le32 bios_build;
  89. u8 flags;
  90. u8 pci_slot;
  91. __le16 ue_num;
  92. __le16 ue_size;
  93. u8 _r[14];
  94. /* The unit element array is right here.
  95. */
  96. } __attribute__ ((packed));
  97. /**
  98. * asd_read_ocm_seg - read an on chip memory (OCM) segment
  99. * @asd_ha: pointer to the host adapter structure
  100. * @buffer: where to write the read data
  101. * @offs: offset into OCM where to read from
  102. * @size: how many bytes to read
  103. *
  104. * Return the number of bytes not read. Return 0 on success.
  105. */
  106. static int asd_read_ocm_seg(struct asd_ha_struct *asd_ha, void *buffer,
  107. u32 offs, int size)
  108. {
  109. u8 *p = buffer;
  110. if (unlikely(asd_ha->iospace))
  111. asd_read_reg_string(asd_ha, buffer, offs+OCM_BASE_ADDR, size);
  112. else {
  113. for ( ; size > 0; size--, offs++, p++)
  114. *p = asd_read_ocm_byte(asd_ha, offs);
  115. }
  116. return size;
  117. }
  118. static int asd_read_ocm_dir(struct asd_ha_struct *asd_ha,
  119. struct asd_ocm_dir *dir, u32 offs)
  120. {
  121. int err = asd_read_ocm_seg(asd_ha, dir, offs, sizeof(*dir));
  122. if (err) {
  123. ASD_DPRINTK("couldn't read ocm segment\n");
  124. return err;
  125. }
  126. if (dir->sig[0] != 'M' || dir->sig[1] != 'O') {
  127. ASD_DPRINTK("no valid dir signature(%c%c) at start of OCM\n",
  128. dir->sig[0], dir->sig[1]);
  129. return -ENOENT;
  130. }
  131. if (dir->major != 0) {
  132. asd_printk("unsupported major version of ocm dir:0x%x\n",
  133. dir->major);
  134. return -ENOENT;
  135. }
  136. dir->num_de &= 0xf;
  137. return 0;
  138. }
  139. /**
  140. * asd_write_ocm_seg - write an on chip memory (OCM) segment
  141. * @asd_ha: pointer to the host adapter structure
  142. * @buffer: where to read the write data
  143. * @offs: offset into OCM to write to
  144. * @size: how many bytes to write
  145. *
  146. * Return the number of bytes not written. Return 0 on success.
  147. */
  148. static void asd_write_ocm_seg(struct asd_ha_struct *asd_ha, void *buffer,
  149. u32 offs, int size)
  150. {
  151. u8 *p = buffer;
  152. if (unlikely(asd_ha->iospace))
  153. asd_write_reg_string(asd_ha, buffer, offs+OCM_BASE_ADDR, size);
  154. else {
  155. for ( ; size > 0; size--, offs++, p++)
  156. asd_write_ocm_byte(asd_ha, offs, *p);
  157. }
  158. return;
  159. }
  160. #define THREE_TO_NUM(X) ((X)[0] | ((X)[1] << 8) | ((X)[2] << 16))
  161. static int asd_find_dir_entry(struct asd_ocm_dir *dir, u8 type,
  162. u32 *offs, u32 *size)
  163. {
  164. int i;
  165. struct asd_ocm_dir_ent *ent;
  166. for (i = 0; i < dir->num_de; i++) {
  167. if (dir->entry[i].type == type)
  168. break;
  169. }
  170. if (i >= dir->num_de)
  171. return -ENOENT;
  172. ent = &dir->entry[i];
  173. *offs = (u32) THREE_TO_NUM(ent->offs);
  174. *size = (u32) THREE_TO_NUM(ent->size);
  175. return 0;
  176. }
  177. #define OCM_BIOS_CHIM_DE 2
  178. #define BC_BIOS_PRESENT 1
  179. static int asd_get_bios_chim(struct asd_ha_struct *asd_ha,
  180. struct asd_ocm_dir *dir)
  181. {
  182. int err;
  183. struct asd_bios_chim_struct *bc_struct;
  184. u32 offs, size;
  185. err = asd_find_dir_entry(dir, OCM_BIOS_CHIM_DE, &offs, &size);
  186. if (err) {
  187. ASD_DPRINTK("couldn't find BIOS_CHIM dir ent\n");
  188. goto out;
  189. }
  190. err = -ENOMEM;
  191. bc_struct = kmalloc(sizeof(*bc_struct), GFP_KERNEL);
  192. if (!bc_struct) {
  193. asd_printk("no memory for bios_chim struct\n");
  194. goto out;
  195. }
  196. err = asd_read_ocm_seg(asd_ha, (void *)bc_struct, offs,
  197. sizeof(*bc_struct));
  198. if (err) {
  199. ASD_DPRINTK("couldn't read ocm segment\n");
  200. goto out2;
  201. }
  202. if (strncmp(bc_struct->sig, "SOIB", 4)
  203. && strncmp(bc_struct->sig, "IPSA", 4)) {
  204. ASD_DPRINTK("BIOS_CHIM entry has no valid sig(%c%c%c%c)\n",
  205. bc_struct->sig[0], bc_struct->sig[1],
  206. bc_struct->sig[2], bc_struct->sig[3]);
  207. err = -ENOENT;
  208. goto out2;
  209. }
  210. if (bc_struct->major != 1) {
  211. asd_printk("BIOS_CHIM unsupported major version:0x%x\n",
  212. bc_struct->major);
  213. err = -ENOENT;
  214. goto out2;
  215. }
  216. if (bc_struct->flags & BC_BIOS_PRESENT) {
  217. asd_ha->hw_prof.bios.present = 1;
  218. asd_ha->hw_prof.bios.maj = bc_struct->bios_major;
  219. asd_ha->hw_prof.bios.min = bc_struct->bios_minor;
  220. asd_ha->hw_prof.bios.bld = le32_to_cpu(bc_struct->bios_build);
  221. ASD_DPRINTK("BIOS present (%d,%d), %d\n",
  222. asd_ha->hw_prof.bios.maj,
  223. asd_ha->hw_prof.bios.min,
  224. asd_ha->hw_prof.bios.bld);
  225. }
  226. asd_ha->hw_prof.ue.num = le16_to_cpu(bc_struct->ue_num);
  227. asd_ha->hw_prof.ue.size= le16_to_cpu(bc_struct->ue_size);
  228. ASD_DPRINTK("ue num:%d, ue size:%d\n", asd_ha->hw_prof.ue.num,
  229. asd_ha->hw_prof.ue.size);
  230. size = asd_ha->hw_prof.ue.num * asd_ha->hw_prof.ue.size;
  231. if (size > 0) {
  232. err = -ENOMEM;
  233. asd_ha->hw_prof.ue.area = kmalloc(size, GFP_KERNEL);
  234. if (!asd_ha->hw_prof.ue.area)
  235. goto out2;
  236. err = asd_read_ocm_seg(asd_ha, (void *)asd_ha->hw_prof.ue.area,
  237. offs + sizeof(*bc_struct), size);
  238. if (err) {
  239. kfree(asd_ha->hw_prof.ue.area);
  240. asd_ha->hw_prof.ue.area = NULL;
  241. asd_ha->hw_prof.ue.num = 0;
  242. asd_ha->hw_prof.ue.size = 0;
  243. ASD_DPRINTK("couldn't read ue entries(%d)\n", err);
  244. }
  245. }
  246. out2:
  247. kfree(bc_struct);
  248. out:
  249. return err;
  250. }
  251. static void
  252. asd_hwi_initialize_ocm_dir (struct asd_ha_struct *asd_ha)
  253. {
  254. int i;
  255. /* Zero OCM */
  256. for (i = 0; i < OCM_MAX_SIZE; i += 4)
  257. asd_write_ocm_dword(asd_ha, i, 0);
  258. /* Write Dir */
  259. asd_write_ocm_seg(asd_ha, &OCMDirInit, 0,
  260. sizeof(struct asd_ocm_dir));
  261. /* Write Dir Entries */
  262. for (i = 0; i < OCM_INIT_DIR_ENTRIES; i++)
  263. asd_write_ocm_seg(asd_ha, &OCMDirEntriesInit[i],
  264. sizeof(struct asd_ocm_dir) +
  265. (i * sizeof(struct asd_ocm_dir_ent))
  266. , sizeof(struct asd_ocm_dir_ent));
  267. }
  268. static int
  269. asd_hwi_check_ocm_access (struct asd_ha_struct *asd_ha)
  270. {
  271. struct pci_dev *pcidev = asd_ha->pcidev;
  272. u32 reg;
  273. int err = 0;
  274. u32 v;
  275. /* check if OCM has been initialized by BIOS */
  276. reg = asd_read_reg_dword(asd_ha, EXSICNFGR);
  277. if (!(reg & OCMINITIALIZED)) {
  278. err = pci_read_config_dword(pcidev, PCIC_INTRPT_STAT, &v);
  279. if (err) {
  280. asd_printk("couldn't access PCIC_INTRPT_STAT of %s\n",
  281. pci_name(pcidev));
  282. goto out;
  283. }
  284. printk(KERN_INFO "OCM is not initialized by BIOS,"
  285. "reinitialize it and ignore it, current IntrptStatus"
  286. "is 0x%x\n", v);
  287. if (v)
  288. err = pci_write_config_dword(pcidev,
  289. PCIC_INTRPT_STAT, v);
  290. if (err) {
  291. asd_printk("couldn't write PCIC_INTRPT_STAT of %s\n",
  292. pci_name(pcidev));
  293. goto out;
  294. }
  295. asd_hwi_initialize_ocm_dir(asd_ha);
  296. }
  297. out:
  298. return err;
  299. }
  300. /**
  301. * asd_read_ocm - read on chip memory (OCM)
  302. * @asd_ha: pointer to the host adapter structure
  303. */
  304. int asd_read_ocm(struct asd_ha_struct *asd_ha)
  305. {
  306. int err;
  307. struct asd_ocm_dir *dir;
  308. if (asd_hwi_check_ocm_access(asd_ha))
  309. return -1;
  310. dir = kmalloc(sizeof(*dir), GFP_KERNEL);
  311. if (!dir) {
  312. asd_printk("no memory for ocm dir\n");
  313. return -ENOMEM;
  314. }
  315. err = asd_read_ocm_dir(asd_ha, dir, 0);
  316. if (err)
  317. goto out;
  318. err = asd_get_bios_chim(asd_ha, dir);
  319. out:
  320. kfree(dir);
  321. return err;
  322. }
  323. /* ---------- FLASH stuff ---------- */
  324. #define FLASH_RESET 0xF0
  325. #define ASD_FLASH_SIZE 0x200000
  326. #define FLASH_DIR_COOKIE "*** ADAPTEC FLASH DIRECTORY *** "
  327. #define FLASH_NEXT_ENTRY_OFFS 0x2000
  328. #define FLASH_MAX_DIR_ENTRIES 32
  329. #define FLASH_DE_TYPE_MASK 0x3FFFFFFF
  330. #define FLASH_DE_MS 0x120
  331. #define FLASH_DE_CTRL_A_USER 0xE0
  332. struct asd_flash_de {
  333. __le32 type;
  334. __le32 offs;
  335. __le32 pad_size;
  336. __le32 image_size;
  337. __le32 chksum;
  338. u8 _r[12];
  339. u8 version[32];
  340. } __attribute__ ((packed));
  341. struct asd_flash_dir {
  342. u8 cookie[32];
  343. __le32 rev; /* 2 */
  344. __le32 chksum;
  345. __le32 chksum_antidote;
  346. __le32 bld;
  347. u8 bld_id[32]; /* build id data */
  348. u8 ver_data[32]; /* date and time of build */
  349. __le32 ae_mask;
  350. __le32 v_mask;
  351. __le32 oc_mask;
  352. u8 _r[20];
  353. struct asd_flash_de dir_entry[FLASH_MAX_DIR_ENTRIES];
  354. } __attribute__ ((packed));
  355. struct asd_manuf_sec {
  356. char sig[2]; /* 'S', 'M' */
  357. u16 offs_next;
  358. u8 maj; /* 0 */
  359. u8 min; /* 0 */
  360. u16 chksum;
  361. u16 size;
  362. u8 _r[6];
  363. u8 sas_addr[SAS_ADDR_SIZE];
  364. u8 pcba_sn[ASD_PCBA_SN_SIZE];
  365. /* Here start the other segments */
  366. u8 linked_list[];
  367. } __attribute__ ((packed));
  368. struct asd_manuf_phy_desc {
  369. u8 state; /* low 4 bits */
  370. #define MS_PHY_STATE_ENABLED 0
  371. #define MS_PHY_STATE_REPORTED 1
  372. #define MS_PHY_STATE_HIDDEN 2
  373. u8 phy_id;
  374. u16 _r;
  375. u8 phy_control_0; /* mode 5 reg 0x160 */
  376. u8 phy_control_1; /* mode 5 reg 0x161 */
  377. u8 phy_control_2; /* mode 5 reg 0x162 */
  378. u8 phy_control_3; /* mode 5 reg 0x163 */
  379. } __attribute__ ((packed));
  380. struct asd_manuf_phy_param {
  381. char sig[2]; /* 'P', 'M' */
  382. u16 next;
  383. u8 maj; /* 0 */
  384. u8 min; /* 2 */
  385. u8 num_phy_desc; /* 8 */
  386. u8 phy_desc_size; /* 8 */
  387. u8 _r[3];
  388. u8 usage_model_id;
  389. u32 _r2;
  390. struct asd_manuf_phy_desc phy_desc[ASD_MAX_PHYS];
  391. } __attribute__ ((packed));
  392. #if 0
  393. static const char *asd_sb_type[] = {
  394. "unknown",
  395. "SGPIO",
  396. [2 ... 0x7F] = "unknown",
  397. [0x80] = "ADPT_I2C",
  398. [0x81 ... 0xFF] = "VENDOR_UNIQUExx"
  399. };
  400. #endif
  401. struct asd_ms_sb_desc {
  402. u8 type;
  403. u8 node_desc_index;
  404. u8 conn_desc_index;
  405. u8 _recvd[];
  406. } __attribute__ ((packed));
  407. #if 0
  408. static const char *asd_conn_type[] = {
  409. [0 ... 7] = "unknown",
  410. "SFF8470",
  411. "SFF8482",
  412. "SFF8484",
  413. [0x80] = "PCIX_DAUGHTER0",
  414. [0x81] = "SAS_DAUGHTER0",
  415. [0x82 ... 0xFF] = "VENDOR_UNIQUExx"
  416. };
  417. static const char *asd_conn_location[] = {
  418. "unknown",
  419. "internal",
  420. "external",
  421. "board_to_board",
  422. };
  423. #endif
  424. struct asd_ms_conn_desc {
  425. u8 type;
  426. u8 location;
  427. u8 num_sideband_desc;
  428. u8 size_sideband_desc;
  429. u32 _resvd;
  430. u8 name[16];
  431. struct asd_ms_sb_desc sb_desc[];
  432. } __attribute__ ((packed));
  433. struct asd_nd_phy_desc {
  434. u8 vp_attch_type;
  435. u8 attch_specific[];
  436. } __attribute__ ((packed));
  437. #if 0
  438. static const char *asd_node_type[] = {
  439. "IOP",
  440. "IO_CONTROLLER",
  441. "EXPANDER",
  442. "PORT_MULTIPLIER",
  443. "PORT_MULTIPLEXER",
  444. "MULTI_DROP_I2C_BUS",
  445. };
  446. #endif
  447. struct asd_ms_node_desc {
  448. u8 type;
  449. u8 num_phy_desc;
  450. u8 size_phy_desc;
  451. u8 _resvd;
  452. u8 name[16];
  453. struct asd_nd_phy_desc phy_desc[];
  454. } __attribute__ ((packed));
  455. struct asd_ms_conn_map {
  456. char sig[2]; /* 'M', 'C' */
  457. __le16 next;
  458. u8 maj; /* 0 */
  459. u8 min; /* 0 */
  460. __le16 cm_size; /* size of this struct */
  461. u8 num_conn;
  462. u8 conn_size;
  463. u8 num_nodes;
  464. u8 usage_model_id;
  465. u32 _resvd;
  466. union {
  467. DECLARE_FLEX_ARRAY(struct asd_ms_conn_desc, conn_desc);
  468. DECLARE_FLEX_ARRAY(struct asd_ms_node_desc, node_desc);
  469. };
  470. } __attribute__ ((packed));
  471. struct asd_ctrla_phy_entry {
  472. u8 sas_addr[SAS_ADDR_SIZE];
  473. u8 sas_link_rates; /* max in hi bits, min in low bits */
  474. u8 flags;
  475. u8 sata_link_rates;
  476. u8 _r[5];
  477. } __attribute__ ((packed));
  478. struct asd_ctrla_phy_settings {
  479. u8 id0; /* P'h'y */
  480. u8 _r;
  481. u16 next;
  482. u8 num_phys; /* number of PHYs in the PCI function */
  483. u8 _r2[3];
  484. struct asd_ctrla_phy_entry phy_ent[ASD_MAX_PHYS];
  485. } __attribute__ ((packed));
  486. struct asd_ll_el {
  487. u8 id0;
  488. u8 id1;
  489. __le16 next;
  490. u8 something_here[];
  491. } __attribute__ ((packed));
  492. static int asd_poll_flash(struct asd_ha_struct *asd_ha)
  493. {
  494. int c;
  495. u8 d;
  496. for (c = 5000; c > 0; c--) {
  497. d = asd_read_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar);
  498. d ^= asd_read_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar);
  499. if (!d)
  500. return 0;
  501. udelay(5);
  502. }
  503. return -ENOENT;
  504. }
  505. static int asd_reset_flash(struct asd_ha_struct *asd_ha)
  506. {
  507. int err;
  508. err = asd_poll_flash(asd_ha);
  509. if (err)
  510. return err;
  511. asd_write_reg_byte(asd_ha, asd_ha->hw_prof.flash.bar, FLASH_RESET);
  512. err = asd_poll_flash(asd_ha);
  513. return err;
  514. }
  515. static int asd_read_flash_seg(struct asd_ha_struct *asd_ha,
  516. void *buffer, u32 offs, int size)
  517. {
  518. asd_read_reg_string(asd_ha, buffer, asd_ha->hw_prof.flash.bar+offs,
  519. size);
  520. return 0;
  521. }
  522. /**
  523. * asd_find_flash_dir - finds and reads the flash directory
  524. * @asd_ha: pointer to the host adapter structure
  525. * @flash_dir: pointer to flash directory structure
  526. *
  527. * If found, the flash directory segment will be copied to
  528. * @flash_dir. Return 1 if found, 0 if not.
  529. */
  530. static int asd_find_flash_dir(struct asd_ha_struct *asd_ha,
  531. struct asd_flash_dir *flash_dir)
  532. {
  533. u32 v;
  534. for (v = 0; v < ASD_FLASH_SIZE; v += FLASH_NEXT_ENTRY_OFFS) {
  535. asd_read_flash_seg(asd_ha, flash_dir, v,
  536. sizeof(FLASH_DIR_COOKIE)-1);
  537. if (memcmp(flash_dir->cookie, FLASH_DIR_COOKIE,
  538. sizeof(FLASH_DIR_COOKIE)-1) == 0) {
  539. asd_ha->hw_prof.flash.dir_offs = v;
  540. asd_read_flash_seg(asd_ha, flash_dir, v,
  541. sizeof(*flash_dir));
  542. return 1;
  543. }
  544. }
  545. return 0;
  546. }
  547. static int asd_flash_getid(struct asd_ha_struct *asd_ha)
  548. {
  549. int err = 0;
  550. u32 reg;
  551. reg = asd_read_reg_dword(asd_ha, EXSICNFGR);
  552. if (pci_read_config_dword(asd_ha->pcidev, PCI_CONF_FLSH_BAR,
  553. &asd_ha->hw_prof.flash.bar)) {
  554. asd_printk("couldn't read PCI_CONF_FLSH_BAR of %s\n",
  555. pci_name(asd_ha->pcidev));
  556. return -ENOENT;
  557. }
  558. asd_ha->hw_prof.flash.present = 1;
  559. asd_ha->hw_prof.flash.wide = reg & FLASHW ? 1 : 0;
  560. err = asd_reset_flash(asd_ha);
  561. if (err) {
  562. ASD_DPRINTK("couldn't reset flash(%d)\n", err);
  563. return err;
  564. }
  565. return 0;
  566. }
  567. static u16 asd_calc_flash_chksum(u16 *p, int size)
  568. {
  569. u16 chksum = 0;
  570. while (size-- > 0)
  571. chksum += *p++;
  572. return chksum;
  573. }
  574. static int asd_find_flash_de(struct asd_flash_dir *flash_dir, u32 entry_type,
  575. u32 *offs, u32 *size)
  576. {
  577. int i;
  578. struct asd_flash_de *de;
  579. for (i = 0; i < FLASH_MAX_DIR_ENTRIES; i++) {
  580. u32 type = le32_to_cpu(flash_dir->dir_entry[i].type);
  581. type &= FLASH_DE_TYPE_MASK;
  582. if (type == entry_type)
  583. break;
  584. }
  585. if (i >= FLASH_MAX_DIR_ENTRIES)
  586. return -ENOENT;
  587. de = &flash_dir->dir_entry[i];
  588. *offs = le32_to_cpu(de->offs);
  589. *size = le32_to_cpu(de->pad_size);
  590. return 0;
  591. }
  592. static int asd_validate_ms(struct asd_manuf_sec *ms)
  593. {
  594. if (ms->sig[0] != 'S' || ms->sig[1] != 'M') {
  595. ASD_DPRINTK("manuf sec: no valid sig(%c%c)\n",
  596. ms->sig[0], ms->sig[1]);
  597. return -ENOENT;
  598. }
  599. if (ms->maj != 0) {
  600. asd_printk("unsupported manuf. sector. major version:%x\n",
  601. ms->maj);
  602. return -ENOENT;
  603. }
  604. ms->offs_next = le16_to_cpu((__force __le16) ms->offs_next);
  605. ms->chksum = le16_to_cpu((__force __le16) ms->chksum);
  606. ms->size = le16_to_cpu((__force __le16) ms->size);
  607. if (asd_calc_flash_chksum((u16 *)ms, ms->size/2)) {
  608. asd_printk("failed manuf sector checksum\n");
  609. }
  610. return 0;
  611. }
  612. static int asd_ms_get_sas_addr(struct asd_ha_struct *asd_ha,
  613. struct asd_manuf_sec *ms)
  614. {
  615. memcpy(asd_ha->hw_prof.sas_addr, ms->sas_addr, SAS_ADDR_SIZE);
  616. return 0;
  617. }
  618. static int asd_ms_get_pcba_sn(struct asd_ha_struct *asd_ha,
  619. struct asd_manuf_sec *ms)
  620. {
  621. memcpy(asd_ha->hw_prof.pcba_sn, ms->pcba_sn, ASD_PCBA_SN_SIZE);
  622. asd_ha->hw_prof.pcba_sn[ASD_PCBA_SN_SIZE] = '\0';
  623. return 0;
  624. }
  625. /**
  626. * asd_find_ll_by_id - find a linked list entry by its id
  627. * @start: void pointer to the first element in the linked list
  628. * @id0: the first byte of the id (offs 0)
  629. * @id1: the second byte of the id (offs 1)
  630. *
  631. * @start has to be the _base_ element start, since the
  632. * linked list entries's offset is from this pointer.
  633. * Some linked list entries use only the first id, in which case
  634. * you can pass 0xFF for the second.
  635. */
  636. static void *asd_find_ll_by_id(void * const start, const u8 id0, const u8 id1)
  637. {
  638. struct asd_ll_el *el = start;
  639. do {
  640. switch (id1) {
  641. default:
  642. if (el->id1 == id1) {
  643. fallthrough;
  644. case 0xFF:
  645. if (el->id0 == id0)
  646. return el;
  647. }
  648. }
  649. el = start + le16_to_cpu(el->next);
  650. } while (el != start);
  651. return NULL;
  652. }
  653. /**
  654. * asd_ms_get_phy_params - get phy parameters from the manufacturing sector
  655. * @asd_ha: pointer to the host adapter structure
  656. * @manuf_sec: pointer to the manufacturing sector
  657. *
  658. * The manufacturing sector contans also the linked list of sub-segments,
  659. * since when it was read, its size was taken from the flash directory,
  660. * not from the structure size.
  661. *
  662. * HIDDEN phys do not count in the total count. REPORTED phys cannot
  663. * be enabled but are reported and counted towards the total.
  664. * ENABLED phys are enabled by default and count towards the total.
  665. * The absolute total phy number is ASD_MAX_PHYS. hw_prof->num_phys
  666. * merely specifies the number of phys the host adapter decided to
  667. * report. E.g., it is possible for phys 0, 1 and 2 to be HIDDEN,
  668. * phys 3, 4 and 5 to be REPORTED and phys 6 and 7 to be ENABLED.
  669. * In this case ASD_MAX_PHYS is 8, hw_prof->num_phys is 5, and only 2
  670. * are actually enabled (enabled by default, max number of phys
  671. * enableable in this case).
  672. */
  673. static int asd_ms_get_phy_params(struct asd_ha_struct *asd_ha,
  674. struct asd_manuf_sec *manuf_sec)
  675. {
  676. int i;
  677. int en_phys = 0;
  678. int rep_phys = 0;
  679. struct asd_manuf_phy_param *phy_param;
  680. struct asd_manuf_phy_param dflt_phy_param;
  681. phy_param = asd_find_ll_by_id(manuf_sec, 'P', 'M');
  682. if (!phy_param) {
  683. ASD_DPRINTK("ms: no phy parameters found\n");
  684. ASD_DPRINTK("ms: Creating default phy parameters\n");
  685. dflt_phy_param.sig[0] = 'P';
  686. dflt_phy_param.sig[1] = 'M';
  687. dflt_phy_param.maj = 0;
  688. dflt_phy_param.min = 2;
  689. dflt_phy_param.num_phy_desc = 8;
  690. dflt_phy_param.phy_desc_size = sizeof(struct asd_manuf_phy_desc);
  691. for (i =0; i < ASD_MAX_PHYS; i++) {
  692. dflt_phy_param.phy_desc[i].state = 0;
  693. dflt_phy_param.phy_desc[i].phy_id = i;
  694. dflt_phy_param.phy_desc[i].phy_control_0 = 0xf6;
  695. dflt_phy_param.phy_desc[i].phy_control_1 = 0x10;
  696. dflt_phy_param.phy_desc[i].phy_control_2 = 0x43;
  697. dflt_phy_param.phy_desc[i].phy_control_3 = 0xeb;
  698. }
  699. phy_param = &dflt_phy_param;
  700. }
  701. if (phy_param->maj != 0) {
  702. asd_printk("unsupported manuf. phy param major version:0x%x\n",
  703. phy_param->maj);
  704. return -ENOENT;
  705. }
  706. ASD_DPRINTK("ms: num_phy_desc: %d\n", phy_param->num_phy_desc);
  707. asd_ha->hw_prof.enabled_phys = 0;
  708. for (i = 0; i < phy_param->num_phy_desc; i++) {
  709. struct asd_manuf_phy_desc *pd = &phy_param->phy_desc[i];
  710. switch (pd->state & 0xF) {
  711. case MS_PHY_STATE_HIDDEN:
  712. ASD_DPRINTK("ms: phy%d: HIDDEN\n", i);
  713. continue;
  714. case MS_PHY_STATE_REPORTED:
  715. ASD_DPRINTK("ms: phy%d: REPORTED\n", i);
  716. asd_ha->hw_prof.enabled_phys &= ~(1 << i);
  717. rep_phys++;
  718. continue;
  719. case MS_PHY_STATE_ENABLED:
  720. ASD_DPRINTK("ms: phy%d: ENABLED\n", i);
  721. asd_ha->hw_prof.enabled_phys |= (1 << i);
  722. en_phys++;
  723. break;
  724. }
  725. asd_ha->hw_prof.phy_desc[i].phy_control_0 = pd->phy_control_0;
  726. asd_ha->hw_prof.phy_desc[i].phy_control_1 = pd->phy_control_1;
  727. asd_ha->hw_prof.phy_desc[i].phy_control_2 = pd->phy_control_2;
  728. asd_ha->hw_prof.phy_desc[i].phy_control_3 = pd->phy_control_3;
  729. }
  730. asd_ha->hw_prof.max_phys = rep_phys + en_phys;
  731. asd_ha->hw_prof.num_phys = en_phys;
  732. ASD_DPRINTK("ms: max_phys:0x%x, num_phys:0x%x\n",
  733. asd_ha->hw_prof.max_phys, asd_ha->hw_prof.num_phys);
  734. ASD_DPRINTK("ms: enabled_phys:0x%x\n", asd_ha->hw_prof.enabled_phys);
  735. return 0;
  736. }
  737. static int asd_ms_get_connector_map(struct asd_ha_struct *asd_ha,
  738. struct asd_manuf_sec *manuf_sec)
  739. {
  740. struct asd_ms_conn_map *cm;
  741. cm = asd_find_ll_by_id(manuf_sec, 'M', 'C');
  742. if (!cm) {
  743. ASD_DPRINTK("ms: no connector map found\n");
  744. return 0;
  745. }
  746. if (cm->maj != 0) {
  747. ASD_DPRINTK("ms: unsupported: connector map major version 0x%x"
  748. "\n", cm->maj);
  749. return -ENOENT;
  750. }
  751. /* XXX */
  752. return 0;
  753. }
  754. /**
  755. * asd_process_ms - find and extract information from the manufacturing sector
  756. * @asd_ha: pointer to the host adapter structure
  757. * @flash_dir: pointer to the flash directory
  758. */
  759. static int asd_process_ms(struct asd_ha_struct *asd_ha,
  760. struct asd_flash_dir *flash_dir)
  761. {
  762. int err;
  763. struct asd_manuf_sec *manuf_sec;
  764. u32 offs, size;
  765. err = asd_find_flash_de(flash_dir, FLASH_DE_MS, &offs, &size);
  766. if (err) {
  767. ASD_DPRINTK("Couldn't find the manuf. sector\n");
  768. goto out;
  769. }
  770. if (size == 0)
  771. goto out;
  772. err = -ENOMEM;
  773. manuf_sec = kmalloc(size, GFP_KERNEL);
  774. if (!manuf_sec) {
  775. ASD_DPRINTK("no mem for manuf sector\n");
  776. goto out;
  777. }
  778. err = asd_read_flash_seg(asd_ha, (void *)manuf_sec, offs, size);
  779. if (err) {
  780. ASD_DPRINTK("couldn't read manuf sector at 0x%x, size 0x%x\n",
  781. offs, size);
  782. goto out2;
  783. }
  784. err = asd_validate_ms(manuf_sec);
  785. if (err) {
  786. ASD_DPRINTK("couldn't validate manuf sector\n");
  787. goto out2;
  788. }
  789. err = asd_ms_get_sas_addr(asd_ha, manuf_sec);
  790. if (err) {
  791. ASD_DPRINTK("couldn't read the SAS_ADDR\n");
  792. goto out2;
  793. }
  794. ASD_DPRINTK("manuf sect SAS_ADDR %llx\n",
  795. SAS_ADDR(asd_ha->hw_prof.sas_addr));
  796. err = asd_ms_get_pcba_sn(asd_ha, manuf_sec);
  797. if (err) {
  798. ASD_DPRINTK("couldn't read the PCBA SN\n");
  799. goto out2;
  800. }
  801. ASD_DPRINTK("manuf sect PCBA SN %s\n", asd_ha->hw_prof.pcba_sn);
  802. err = asd_ms_get_phy_params(asd_ha, manuf_sec);
  803. if (err) {
  804. ASD_DPRINTK("ms: couldn't get phy parameters\n");
  805. goto out2;
  806. }
  807. err = asd_ms_get_connector_map(asd_ha, manuf_sec);
  808. if (err) {
  809. ASD_DPRINTK("ms: couldn't get connector map\n");
  810. goto out2;
  811. }
  812. out2:
  813. kfree(manuf_sec);
  814. out:
  815. return err;
  816. }
  817. static int asd_process_ctrla_phy_settings(struct asd_ha_struct *asd_ha,
  818. struct asd_ctrla_phy_settings *ps)
  819. {
  820. int i;
  821. for (i = 0; i < ps->num_phys; i++) {
  822. struct asd_ctrla_phy_entry *pe = &ps->phy_ent[i];
  823. if (!PHY_ENABLED(asd_ha, i))
  824. continue;
  825. if (*(u64 *)pe->sas_addr == 0) {
  826. asd_ha->hw_prof.enabled_phys &= ~(1 << i);
  827. continue;
  828. }
  829. /* This is the SAS address which should be sent in IDENTIFY. */
  830. memcpy(asd_ha->hw_prof.phy_desc[i].sas_addr, pe->sas_addr,
  831. SAS_ADDR_SIZE);
  832. asd_ha->hw_prof.phy_desc[i].max_sas_lrate =
  833. (pe->sas_link_rates & 0xF0) >> 4;
  834. asd_ha->hw_prof.phy_desc[i].min_sas_lrate =
  835. (pe->sas_link_rates & 0x0F);
  836. asd_ha->hw_prof.phy_desc[i].max_sata_lrate =
  837. (pe->sata_link_rates & 0xF0) >> 4;
  838. asd_ha->hw_prof.phy_desc[i].min_sata_lrate =
  839. (pe->sata_link_rates & 0x0F);
  840. asd_ha->hw_prof.phy_desc[i].flags = pe->flags;
  841. ASD_DPRINTK("ctrla: phy%d: sas_addr: %llx, sas rate:0x%x-0x%x,"
  842. " sata rate:0x%x-0x%x, flags:0x%x\n",
  843. i,
  844. SAS_ADDR(asd_ha->hw_prof.phy_desc[i].sas_addr),
  845. asd_ha->hw_prof.phy_desc[i].max_sas_lrate,
  846. asd_ha->hw_prof.phy_desc[i].min_sas_lrate,
  847. asd_ha->hw_prof.phy_desc[i].max_sata_lrate,
  848. asd_ha->hw_prof.phy_desc[i].min_sata_lrate,
  849. asd_ha->hw_prof.phy_desc[i].flags);
  850. }
  851. return 0;
  852. }
  853. /**
  854. * asd_process_ctrl_a_user - process CTRL-A user settings
  855. * @asd_ha: pointer to the host adapter structure
  856. * @flash_dir: pointer to the flash directory
  857. */
  858. static int asd_process_ctrl_a_user(struct asd_ha_struct *asd_ha,
  859. struct asd_flash_dir *flash_dir)
  860. {
  861. int err, i;
  862. u32 offs, size;
  863. struct asd_ll_el *el = NULL;
  864. struct asd_ctrla_phy_settings *ps;
  865. struct asd_ctrla_phy_settings dflt_ps;
  866. err = asd_find_flash_de(flash_dir, FLASH_DE_CTRL_A_USER, &offs, &size);
  867. if (err) {
  868. ASD_DPRINTK("couldn't find CTRL-A user settings section\n");
  869. ASD_DPRINTK("Creating default CTRL-A user settings section\n");
  870. dflt_ps.id0 = 'h';
  871. dflt_ps.num_phys = 8;
  872. for (i =0; i < ASD_MAX_PHYS; i++) {
  873. memcpy(dflt_ps.phy_ent[i].sas_addr,
  874. asd_ha->hw_prof.sas_addr, SAS_ADDR_SIZE);
  875. dflt_ps.phy_ent[i].sas_link_rates = 0x98;
  876. dflt_ps.phy_ent[i].flags = 0x0;
  877. dflt_ps.phy_ent[i].sata_link_rates = 0x0;
  878. }
  879. size = sizeof(struct asd_ctrla_phy_settings);
  880. ps = &dflt_ps;
  881. goto out_process;
  882. }
  883. if (size == 0)
  884. goto out;
  885. err = -ENOMEM;
  886. el = kmalloc(size, GFP_KERNEL);
  887. if (!el) {
  888. ASD_DPRINTK("no mem for ctrla user settings section\n");
  889. goto out;
  890. }
  891. err = asd_read_flash_seg(asd_ha, (void *)el, offs, size);
  892. if (err) {
  893. ASD_DPRINTK("couldn't read ctrla phy settings section\n");
  894. goto out2;
  895. }
  896. err = -ENOENT;
  897. ps = asd_find_ll_by_id(el, 'h', 0xFF);
  898. if (!ps) {
  899. ASD_DPRINTK("couldn't find ctrla phy settings struct\n");
  900. goto out2;
  901. }
  902. out_process:
  903. err = asd_process_ctrla_phy_settings(asd_ha, ps);
  904. if (err) {
  905. ASD_DPRINTK("couldn't process ctrla phy settings\n");
  906. goto out2;
  907. }
  908. out2:
  909. kfree(el);
  910. out:
  911. return err;
  912. }
  913. /**
  914. * asd_read_flash - read flash memory
  915. * @asd_ha: pointer to the host adapter structure
  916. */
  917. int asd_read_flash(struct asd_ha_struct *asd_ha)
  918. {
  919. int err;
  920. struct asd_flash_dir *flash_dir;
  921. err = asd_flash_getid(asd_ha);
  922. if (err)
  923. return err;
  924. flash_dir = kmalloc(sizeof(*flash_dir), GFP_KERNEL);
  925. if (!flash_dir)
  926. return -ENOMEM;
  927. err = -ENOENT;
  928. if (!asd_find_flash_dir(asd_ha, flash_dir)) {
  929. ASD_DPRINTK("couldn't find flash directory\n");
  930. goto out;
  931. }
  932. if (le32_to_cpu(flash_dir->rev) != 2) {
  933. asd_printk("unsupported flash dir version:0x%x\n",
  934. le32_to_cpu(flash_dir->rev));
  935. goto out;
  936. }
  937. err = asd_process_ms(asd_ha, flash_dir);
  938. if (err) {
  939. ASD_DPRINTK("couldn't process manuf sector settings\n");
  940. goto out;
  941. }
  942. err = asd_process_ctrl_a_user(asd_ha, flash_dir);
  943. if (err) {
  944. ASD_DPRINTK("couldn't process CTRL-A user settings\n");
  945. goto out;
  946. }
  947. out:
  948. kfree(flash_dir);
  949. return err;
  950. }
  951. /**
  952. * asd_verify_flash_seg - verify data with flash memory
  953. * @asd_ha: pointer to the host adapter structure
  954. * @src: pointer to the source data to be verified
  955. * @dest_offset: offset from flash memory
  956. * @bytes_to_verify: total bytes to verify
  957. */
  958. int asd_verify_flash_seg(struct asd_ha_struct *asd_ha,
  959. const void *src, u32 dest_offset, u32 bytes_to_verify)
  960. {
  961. const u8 *src_buf;
  962. u8 flash_char;
  963. int err;
  964. u32 nv_offset, reg, i;
  965. reg = asd_ha->hw_prof.flash.bar;
  966. src_buf = NULL;
  967. err = FLASH_OK;
  968. nv_offset = dest_offset;
  969. src_buf = (const u8 *)src;
  970. for (i = 0; i < bytes_to_verify; i++) {
  971. flash_char = asd_read_reg_byte(asd_ha, reg + nv_offset + i);
  972. if (flash_char != src_buf[i]) {
  973. err = FAIL_VERIFY;
  974. break;
  975. }
  976. }
  977. return err;
  978. }
  979. /**
  980. * asd_write_flash_seg - write data into flash memory
  981. * @asd_ha: pointer to the host adapter structure
  982. * @src: pointer to the source data to be written
  983. * @dest_offset: offset from flash memory
  984. * @bytes_to_write: total bytes to write
  985. */
  986. int asd_write_flash_seg(struct asd_ha_struct *asd_ha,
  987. const void *src, u32 dest_offset, u32 bytes_to_write)
  988. {
  989. const u8 *src_buf;
  990. u32 nv_offset, reg, i;
  991. int err;
  992. reg = asd_ha->hw_prof.flash.bar;
  993. src_buf = NULL;
  994. err = asd_check_flash_type(asd_ha);
  995. if (err) {
  996. ASD_DPRINTK("couldn't find the type of flash. err=%d\n", err);
  997. return err;
  998. }
  999. nv_offset = dest_offset;
  1000. err = asd_erase_nv_sector(asd_ha, nv_offset, bytes_to_write);
  1001. if (err) {
  1002. ASD_DPRINTK("Erase failed at offset:0x%x\n",
  1003. nv_offset);
  1004. return err;
  1005. }
  1006. err = asd_reset_flash(asd_ha);
  1007. if (err) {
  1008. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1009. return err;
  1010. }
  1011. src_buf = (const u8 *)src;
  1012. for (i = 0; i < bytes_to_write; i++) {
  1013. /* Setup program command sequence */
  1014. switch (asd_ha->hw_prof.flash.method) {
  1015. case FLASH_METHOD_A:
  1016. {
  1017. asd_write_reg_byte(asd_ha,
  1018. (reg + 0xAAA), 0xAA);
  1019. asd_write_reg_byte(asd_ha,
  1020. (reg + 0x555), 0x55);
  1021. asd_write_reg_byte(asd_ha,
  1022. (reg + 0xAAA), 0xA0);
  1023. asd_write_reg_byte(asd_ha,
  1024. (reg + nv_offset + i),
  1025. (*(src_buf + i)));
  1026. break;
  1027. }
  1028. case FLASH_METHOD_B:
  1029. {
  1030. asd_write_reg_byte(asd_ha,
  1031. (reg + 0x555), 0xAA);
  1032. asd_write_reg_byte(asd_ha,
  1033. (reg + 0x2AA), 0x55);
  1034. asd_write_reg_byte(asd_ha,
  1035. (reg + 0x555), 0xA0);
  1036. asd_write_reg_byte(asd_ha,
  1037. (reg + nv_offset + i),
  1038. (*(src_buf + i)));
  1039. break;
  1040. }
  1041. default:
  1042. break;
  1043. }
  1044. if (asd_chk_write_status(asd_ha,
  1045. (nv_offset + i), 0) != 0) {
  1046. ASD_DPRINTK("aicx: Write failed at offset:0x%x\n",
  1047. reg + nv_offset + i);
  1048. return FAIL_WRITE_FLASH;
  1049. }
  1050. }
  1051. err = asd_reset_flash(asd_ha);
  1052. if (err) {
  1053. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1054. return err;
  1055. }
  1056. return 0;
  1057. }
  1058. int asd_chk_write_status(struct asd_ha_struct *asd_ha,
  1059. u32 sector_addr, u8 erase_flag)
  1060. {
  1061. u32 reg;
  1062. u32 loop_cnt;
  1063. u8 nv_data1, nv_data2;
  1064. u8 toggle_bit1;
  1065. /*
  1066. * Read from DQ2 requires sector address
  1067. * while it's dont care for DQ6
  1068. */
  1069. reg = asd_ha->hw_prof.flash.bar;
  1070. for (loop_cnt = 0; loop_cnt < 50000; loop_cnt++) {
  1071. nv_data1 = asd_read_reg_byte(asd_ha, reg);
  1072. nv_data2 = asd_read_reg_byte(asd_ha, reg);
  1073. toggle_bit1 = ((nv_data1 & FLASH_STATUS_BIT_MASK_DQ6)
  1074. ^ (nv_data2 & FLASH_STATUS_BIT_MASK_DQ6));
  1075. if (toggle_bit1 == 0) {
  1076. return 0;
  1077. } else {
  1078. if (nv_data2 & FLASH_STATUS_BIT_MASK_DQ5) {
  1079. nv_data1 = asd_read_reg_byte(asd_ha,
  1080. reg);
  1081. nv_data2 = asd_read_reg_byte(asd_ha,
  1082. reg);
  1083. toggle_bit1 =
  1084. ((nv_data1 & FLASH_STATUS_BIT_MASK_DQ6)
  1085. ^ (nv_data2 & FLASH_STATUS_BIT_MASK_DQ6));
  1086. if (toggle_bit1 == 0)
  1087. return 0;
  1088. }
  1089. }
  1090. /*
  1091. * ERASE is a sector-by-sector operation and requires
  1092. * more time to finish while WRITE is byte-byte-byte
  1093. * operation and takes lesser time to finish.
  1094. *
  1095. * For some strange reason a reduced ERASE delay gives different
  1096. * behaviour across different spirit boards. Hence we set
  1097. * a optimum balance of 50mus for ERASE which works well
  1098. * across all boards.
  1099. */
  1100. if (erase_flag) {
  1101. udelay(FLASH_STATUS_ERASE_DELAY_COUNT);
  1102. } else {
  1103. udelay(FLASH_STATUS_WRITE_DELAY_COUNT);
  1104. }
  1105. }
  1106. return -1;
  1107. }
  1108. /**
  1109. * asd_erase_nv_sector - Erase the flash memory sectors.
  1110. * @asd_ha: pointer to the host adapter structure
  1111. * @flash_addr: pointer to offset from flash memory
  1112. * @size: total bytes to erase.
  1113. */
  1114. int asd_erase_nv_sector(struct asd_ha_struct *asd_ha, u32 flash_addr, u32 size)
  1115. {
  1116. u32 reg;
  1117. u32 sector_addr;
  1118. reg = asd_ha->hw_prof.flash.bar;
  1119. /* sector staring address */
  1120. sector_addr = flash_addr & FLASH_SECTOR_SIZE_MASK;
  1121. /*
  1122. * Erasing an flash sector needs to be done in six consecutive
  1123. * write cyles.
  1124. */
  1125. while (sector_addr < flash_addr+size) {
  1126. switch (asd_ha->hw_prof.flash.method) {
  1127. case FLASH_METHOD_A:
  1128. asd_write_reg_byte(asd_ha, (reg + 0xAAA), 0xAA);
  1129. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x55);
  1130. asd_write_reg_byte(asd_ha, (reg + 0xAAA), 0x80);
  1131. asd_write_reg_byte(asd_ha, (reg + 0xAAA), 0xAA);
  1132. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x55);
  1133. asd_write_reg_byte(asd_ha, (reg + sector_addr), 0x30);
  1134. break;
  1135. case FLASH_METHOD_B:
  1136. asd_write_reg_byte(asd_ha, (reg + 0x555), 0xAA);
  1137. asd_write_reg_byte(asd_ha, (reg + 0x2AA), 0x55);
  1138. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x80);
  1139. asd_write_reg_byte(asd_ha, (reg + 0x555), 0xAA);
  1140. asd_write_reg_byte(asd_ha, (reg + 0x2AA), 0x55);
  1141. asd_write_reg_byte(asd_ha, (reg + sector_addr), 0x30);
  1142. break;
  1143. default:
  1144. break;
  1145. }
  1146. if (asd_chk_write_status(asd_ha, sector_addr, 1) != 0)
  1147. return FAIL_ERASE_FLASH;
  1148. sector_addr += FLASH_SECTOR_SIZE;
  1149. }
  1150. return 0;
  1151. }
  1152. int asd_check_flash_type(struct asd_ha_struct *asd_ha)
  1153. {
  1154. u8 manuf_id;
  1155. u8 dev_id;
  1156. u8 sec_prot;
  1157. u32 inc;
  1158. u32 reg;
  1159. int err;
  1160. /* get Flash memory base address */
  1161. reg = asd_ha->hw_prof.flash.bar;
  1162. /* Determine flash info */
  1163. err = asd_reset_flash(asd_ha);
  1164. if (err) {
  1165. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1166. return err;
  1167. }
  1168. asd_ha->hw_prof.flash.method = FLASH_METHOD_UNKNOWN;
  1169. asd_ha->hw_prof.flash.manuf = FLASH_MANUF_ID_UNKNOWN;
  1170. asd_ha->hw_prof.flash.dev_id = FLASH_DEV_ID_UNKNOWN;
  1171. /* Get flash info. This would most likely be AMD Am29LV family flash.
  1172. * First try the sequence for word mode. It is the same as for
  1173. * 008B (byte mode only), 160B (word mode) and 800D (word mode).
  1174. */
  1175. inc = asd_ha->hw_prof.flash.wide ? 2 : 1;
  1176. asd_write_reg_byte(asd_ha, reg + 0xAAA, 0xAA);
  1177. asd_write_reg_byte(asd_ha, reg + 0x555, 0x55);
  1178. asd_write_reg_byte(asd_ha, reg + 0xAAA, 0x90);
  1179. manuf_id = asd_read_reg_byte(asd_ha, reg);
  1180. dev_id = asd_read_reg_byte(asd_ha, reg + inc);
  1181. sec_prot = asd_read_reg_byte(asd_ha, reg + inc + inc);
  1182. /* Get out of autoselect mode. */
  1183. err = asd_reset_flash(asd_ha);
  1184. if (err) {
  1185. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1186. return err;
  1187. }
  1188. ASD_DPRINTK("Flash MethodA manuf_id(0x%x) dev_id(0x%x) "
  1189. "sec_prot(0x%x)\n", manuf_id, dev_id, sec_prot);
  1190. err = asd_reset_flash(asd_ha);
  1191. if (err != 0)
  1192. return err;
  1193. switch (manuf_id) {
  1194. case FLASH_MANUF_ID_AMD:
  1195. switch (sec_prot) {
  1196. case FLASH_DEV_ID_AM29LV800DT:
  1197. case FLASH_DEV_ID_AM29LV640MT:
  1198. case FLASH_DEV_ID_AM29F800B:
  1199. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1200. break;
  1201. default:
  1202. break;
  1203. }
  1204. break;
  1205. case FLASH_MANUF_ID_ST:
  1206. switch (sec_prot) {
  1207. case FLASH_DEV_ID_STM29W800DT:
  1208. case FLASH_DEV_ID_STM29LV640:
  1209. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1210. break;
  1211. default:
  1212. break;
  1213. }
  1214. break;
  1215. case FLASH_MANUF_ID_FUJITSU:
  1216. switch (sec_prot) {
  1217. case FLASH_DEV_ID_MBM29LV800TE:
  1218. case FLASH_DEV_ID_MBM29DL800TA:
  1219. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1220. break;
  1221. }
  1222. break;
  1223. case FLASH_MANUF_ID_MACRONIX:
  1224. switch (sec_prot) {
  1225. case FLASH_DEV_ID_MX29LV800BT:
  1226. asd_ha->hw_prof.flash.method = FLASH_METHOD_A;
  1227. break;
  1228. }
  1229. break;
  1230. }
  1231. if (asd_ha->hw_prof.flash.method == FLASH_METHOD_UNKNOWN) {
  1232. err = asd_reset_flash(asd_ha);
  1233. if (err) {
  1234. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1235. return err;
  1236. }
  1237. /* Issue Unlock sequence for AM29LV008BT */
  1238. asd_write_reg_byte(asd_ha, (reg + 0x555), 0xAA);
  1239. asd_write_reg_byte(asd_ha, (reg + 0x2AA), 0x55);
  1240. asd_write_reg_byte(asd_ha, (reg + 0x555), 0x90);
  1241. manuf_id = asd_read_reg_byte(asd_ha, reg);
  1242. dev_id = asd_read_reg_byte(asd_ha, reg + inc);
  1243. sec_prot = asd_read_reg_byte(asd_ha, reg + inc + inc);
  1244. ASD_DPRINTK("Flash MethodB manuf_id(0x%x) dev_id(0x%x) sec_prot"
  1245. "(0x%x)\n", manuf_id, dev_id, sec_prot);
  1246. err = asd_reset_flash(asd_ha);
  1247. if (err != 0) {
  1248. ASD_DPRINTK("couldn't reset flash. err=%d\n", err);
  1249. return err;
  1250. }
  1251. switch (manuf_id) {
  1252. case FLASH_MANUF_ID_AMD:
  1253. switch (dev_id) {
  1254. case FLASH_DEV_ID_AM29LV008BT:
  1255. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1256. break;
  1257. default:
  1258. break;
  1259. }
  1260. break;
  1261. case FLASH_MANUF_ID_ST:
  1262. switch (dev_id) {
  1263. case FLASH_DEV_ID_STM29008:
  1264. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1265. break;
  1266. default:
  1267. break;
  1268. }
  1269. break;
  1270. case FLASH_MANUF_ID_FUJITSU:
  1271. switch (dev_id) {
  1272. case FLASH_DEV_ID_MBM29LV008TA:
  1273. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1274. break;
  1275. }
  1276. break;
  1277. case FLASH_MANUF_ID_INTEL:
  1278. switch (dev_id) {
  1279. case FLASH_DEV_ID_I28LV00TAT:
  1280. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1281. break;
  1282. }
  1283. break;
  1284. case FLASH_MANUF_ID_MACRONIX:
  1285. switch (dev_id) {
  1286. case FLASH_DEV_ID_I28LV00TAT:
  1287. asd_ha->hw_prof.flash.method = FLASH_METHOD_B;
  1288. break;
  1289. }
  1290. break;
  1291. default:
  1292. return FAIL_FIND_FLASH_ID;
  1293. }
  1294. }
  1295. if (asd_ha->hw_prof.flash.method == FLASH_METHOD_UNKNOWN)
  1296. return FAIL_FIND_FLASH_ID;
  1297. asd_ha->hw_prof.flash.manuf = manuf_id;
  1298. asd_ha->hw_prof.flash.dev_id = dev_id;
  1299. asd_ha->hw_prof.flash.sec_prot = sec_prot;
  1300. return 0;
  1301. }