eprom.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright(c) 2015, 2016 Intel Corporation.
  3. *
  4. * This file is provided under a dual BSD/GPLv2 license. When using or
  5. * redistributing this file, you may do so under either license.
  6. *
  7. * GPL LICENSE SUMMARY
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * BSD LICENSE
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions
  22. * are met:
  23. *
  24. * - Redistributions of source code must retain the above copyright
  25. * notice, this list of conditions and the following disclaimer.
  26. * - Redistributions in binary form must reproduce the above copyright
  27. * notice, this list of conditions and the following disclaimer in
  28. * the documentation and/or other materials provided with the
  29. * distribution.
  30. * - Neither the name of Intel Corporation nor the names of its
  31. * contributors may be used to endorse or promote products derived
  32. * from this software without specific prior written permission.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  35. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  37. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  38. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  39. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  40. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  41. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  42. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  43. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  44. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. *
  46. */
  47. #include <linux/delay.h>
  48. #include "hfi.h"
  49. #include "common.h"
  50. #include "eprom.h"
  51. /*
  52. * The EPROM is logically divided into three partitions:
  53. * partition 0: the first 128K, visible from PCI ROM BAR
  54. * partition 1: 4K config file (sector size)
  55. * partition 2: the rest
  56. */
  57. #define P0_SIZE (128 * 1024)
  58. #define P1_SIZE (4 * 1024)
  59. #define P1_START P0_SIZE
  60. #define P2_START (P0_SIZE + P1_SIZE)
  61. /* controller page size, in bytes */
  62. #define EP_PAGE_SIZE 256
  63. #define EP_PAGE_MASK (EP_PAGE_SIZE - 1)
  64. #define EP_PAGE_DWORDS (EP_PAGE_SIZE / sizeof(u32))
  65. /* controller commands */
  66. #define CMD_SHIFT 24
  67. #define CMD_NOP (0)
  68. #define CMD_READ_DATA(addr) ((0x03 << CMD_SHIFT) | addr)
  69. #define CMD_RELEASE_POWERDOWN_NOID ((0xab << CMD_SHIFT))
  70. /* controller interface speeds */
  71. #define EP_SPEED_FULL 0x2 /* full speed */
  72. /*
  73. * How long to wait for the EPROM to become available, in ms.
  74. * The spec 32 Mb EPROM takes around 40s to erase then write.
  75. * Double it for safety.
  76. */
  77. #define EPROM_TIMEOUT 80000 /* ms */
  78. /*
  79. * Read a 256 byte (64 dword) EPROM page.
  80. * All callers have verified the offset is at a page boundary.
  81. */
  82. static void read_page(struct hfi1_devdata *dd, u32 offset, u32 *result)
  83. {
  84. int i;
  85. write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_READ_DATA(offset));
  86. for (i = 0; i < EP_PAGE_DWORDS; i++)
  87. result[i] = (u32)read_csr(dd, ASIC_EEP_DATA);
  88. write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_NOP); /* close open page */
  89. }
  90. /*
  91. * Read length bytes starting at offset from the start of the EPROM.
  92. */
  93. static int read_length(struct hfi1_devdata *dd, u32 start, u32 len, void *dest)
  94. {
  95. u32 buffer[EP_PAGE_DWORDS];
  96. u32 end;
  97. u32 start_offset;
  98. u32 read_start;
  99. u32 bytes;
  100. if (len == 0)
  101. return 0;
  102. end = start + len;
  103. /*
  104. * Make sure the read range is not outside of the controller read
  105. * command address range. Note that '>' is correct below - the end
  106. * of the range is OK if it stops at the limit, but no higher.
  107. */
  108. if (end > (1 << CMD_SHIFT))
  109. return -EINVAL;
  110. /* read the first partial page */
  111. start_offset = start & EP_PAGE_MASK;
  112. if (start_offset) {
  113. /* partial starting page */
  114. /* align and read the page that contains the start */
  115. read_start = start & ~EP_PAGE_MASK;
  116. read_page(dd, read_start, buffer);
  117. /* the rest of the page is available data */
  118. bytes = EP_PAGE_SIZE - start_offset;
  119. if (len <= bytes) {
  120. /* end is within this page */
  121. memcpy(dest, (u8 *)buffer + start_offset, len);
  122. return 0;
  123. }
  124. memcpy(dest, (u8 *)buffer + start_offset, bytes);
  125. start += bytes;
  126. len -= bytes;
  127. dest += bytes;
  128. }
  129. /* start is now page aligned */
  130. /* read whole pages */
  131. while (len >= EP_PAGE_SIZE) {
  132. read_page(dd, start, buffer);
  133. memcpy(dest, buffer, EP_PAGE_SIZE);
  134. start += EP_PAGE_SIZE;
  135. len -= EP_PAGE_SIZE;
  136. dest += EP_PAGE_SIZE;
  137. }
  138. /* read the last partial page */
  139. if (len) {
  140. read_page(dd, start, buffer);
  141. memcpy(dest, buffer, len);
  142. }
  143. return 0;
  144. }
  145. /*
  146. * Initialize the EPROM handler.
  147. */
  148. int eprom_init(struct hfi1_devdata *dd)
  149. {
  150. int ret = 0;
  151. /* only the discrete chip has an EPROM */
  152. if (dd->pcidev->device != PCI_DEVICE_ID_INTEL0)
  153. return 0;
  154. /*
  155. * It is OK if both HFIs reset the EPROM as long as they don't
  156. * do it at the same time.
  157. */
  158. ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
  159. if (ret) {
  160. dd_dev_err(dd,
  161. "%s: unable to acquire EPROM resource, no EPROM support\n",
  162. __func__);
  163. goto done_asic;
  164. }
  165. /* reset EPROM to be sure it is in a good state */
  166. /* set reset */
  167. write_csr(dd, ASIC_EEP_CTL_STAT, ASIC_EEP_CTL_STAT_EP_RESET_SMASK);
  168. /* clear reset, set speed */
  169. write_csr(dd, ASIC_EEP_CTL_STAT,
  170. EP_SPEED_FULL << ASIC_EEP_CTL_STAT_RATE_SPI_SHIFT);
  171. /* wake the device with command "release powerdown NoID" */
  172. write_csr(dd, ASIC_EEP_ADDR_CMD, CMD_RELEASE_POWERDOWN_NOID);
  173. dd->eprom_available = true;
  174. release_chip_resource(dd, CR_EPROM);
  175. done_asic:
  176. return ret;
  177. }
  178. /* magic character sequence that trails an image */
  179. #define IMAGE_TRAIL_MAGIC "egamiAPO"
  180. /*
  181. * Read all of partition 1. The actual file is at the front. Adjust
  182. * the returned size if a trailing image magic is found.
  183. */
  184. static int read_partition_platform_config(struct hfi1_devdata *dd, void **data,
  185. u32 *size)
  186. {
  187. void *buffer;
  188. void *p;
  189. u32 length;
  190. int ret;
  191. buffer = kmalloc(P1_SIZE, GFP_KERNEL);
  192. if (!buffer)
  193. return -ENOMEM;
  194. ret = read_length(dd, P1_START, P1_SIZE, buffer);
  195. if (ret) {
  196. kfree(buffer);
  197. return ret;
  198. }
  199. /* scan for image magic that may trail the actual data */
  200. p = strnstr(buffer, IMAGE_TRAIL_MAGIC, P1_SIZE);
  201. if (p)
  202. length = p - buffer;
  203. else
  204. length = P1_SIZE;
  205. *data = buffer;
  206. *size = length;
  207. return 0;
  208. }
  209. /*
  210. * Read the platform configuration file from the EPROM.
  211. *
  212. * On success, an allocated buffer containing the data and its size are
  213. * returned. It is up to the caller to free this buffer.
  214. *
  215. * Return value:
  216. * 0 - success
  217. * -ENXIO - no EPROM is available
  218. * -EBUSY - not able to acquire access to the EPROM
  219. * -ENOENT - no recognizable file written
  220. * -ENOMEM - buffer could not be allocated
  221. */
  222. int eprom_read_platform_config(struct hfi1_devdata *dd, void **data, u32 *size)
  223. {
  224. u32 directory[EP_PAGE_DWORDS]; /* aligned buffer */
  225. int ret;
  226. if (!dd->eprom_available)
  227. return -ENXIO;
  228. ret = acquire_chip_resource(dd, CR_EPROM, EPROM_TIMEOUT);
  229. if (ret)
  230. return -EBUSY;
  231. /* read the last page of P0 for the EPROM format magic */
  232. ret = read_length(dd, P1_START - EP_PAGE_SIZE, EP_PAGE_SIZE, directory);
  233. if (ret)
  234. goto done;
  235. /* last dword of P0 contains a magic indicator */
  236. if (directory[EP_PAGE_DWORDS - 1] == 0) {
  237. /* partition format */
  238. ret = read_partition_platform_config(dd, data, size);
  239. goto done;
  240. }
  241. /* nothing recognized */
  242. ret = -ENOENT;
  243. done:
  244. release_chip_resource(dd, CR_EPROM);
  245. return ret;
  246. }