3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2015 Intel Corporation.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
21 * Copyright(c) 2015 Intel Corporation.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
27 * - Redistributions of source code must retain the above copyright
28 * notice, this list of conditions and the following disclaimer.
29 * - Redistributions in binary form must reproduce the above copyright
30 * notice, this list of conditions and the following disclaimer in
31 * the documentation and/or other materials provided with the
33 * - Neither the name of Intel Corporation nor the names of its
34 * contributors may be used to endorse or promote products derived
35 * from this software without specific prior written permission.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 #include <linux/delay.h>
56 * The EPROM is logically divided into three partitions:
57 * partition 0: the first 128K, visible from PCI ROM BAR
58 * partition 1: 4K config file (sector size)
59 * partition 2: the rest
61 #define P0_SIZE (128 * 1024)
62 #define P1_SIZE (4 * 1024)
63 #define P1_START P0_SIZE
64 #define P2_START (P0_SIZE + P1_SIZE)
66 /* erase sizes supported by the controller */
67 #define SIZE_4KB (4 * 1024)
68 #define MASK_4KB (SIZE_4KB - 1)
70 #define SIZE_32KB (32 * 1024)
71 #define MASK_32KB (SIZE_32KB - 1)
73 #define SIZE_64KB (64 * 1024)
74 #define MASK_64KB (SIZE_64KB - 1)
76 /* controller page size, in bytes */
77 #define EP_PAGE_SIZE 256
78 #define EEP_PAGE_MASK (EP_PAGE_SIZE - 1)
80 /* controller commands */
83 #define CMD_PAGE_PROGRAM(addr) ((0x02 << CMD_SHIFT) | addr)
84 #define CMD_READ_DATA(addr) ((0x03 << CMD_SHIFT) | addr)
85 #define CMD_READ_SR1 ((0x05 << CMD_SHIFT))
86 #define CMD_WRITE_ENABLE ((0x06 << CMD_SHIFT))
87 #define CMD_SECTOR_ERASE_4KB(addr) ((0x20 << CMD_SHIFT) | addr)
88 #define CMD_SECTOR_ERASE_32KB(addr) ((0x52 << CMD_SHIFT) | addr)
89 #define CMD_CHIP_ERASE ((0x60 << CMD_SHIFT))
90 #define CMD_READ_MANUF_DEV_ID ((0x90 << CMD_SHIFT))
91 #define CMD_RELEASE_POWERDOWN_NOID ((0xab << CMD_SHIFT))
92 #define CMD_SECTOR_ERASE_64KB(addr) ((0xd8 << CMD_SHIFT) | addr)
94 /* controller interface speeds */
95 #define EP_SPEED_FULL 0x2 /* full speed */
97 /* controller status register 1 bits */
98 #define SR1_BUSY 0x1ull /* the BUSY bit in SR1 */
100 /* sleep length while waiting for controller */
101 #define WAIT_SLEEP_US 100 /* must be larger than 5 (see usage) */
102 #define COUNT_DELAY_SEC(n) ((n) * (1000000/WAIT_SLEEP_US))
105 #define EPROM_WP_N (1ull << 14) /* EPROM write line */
108 * Use the EP mutex to guard against other callers from within the driver.
109 * Also covers usage of eprom_available.
111 static DEFINE_MUTEX(eprom_mutex
);
112 static int eprom_available
; /* default: not available */
115 * Turn on external enable line that allows writing on the flash.
117 static void write_enable(struct hfi1_devdata
*dd
)
120 write_csr(dd
, ASIC_GPIO_OUT
,
121 read_csr(dd
, ASIC_GPIO_OUT
) | EPROM_WP_N
);
123 write_csr(dd
, ASIC_GPIO_OE
,
124 read_csr(dd
, ASIC_GPIO_OE
) | EPROM_WP_N
);
128 * Turn off external enable line that allows writing on the flash.
130 static void write_disable(struct hfi1_devdata
*dd
)
133 write_csr(dd
, ASIC_GPIO_OUT
,
134 read_csr(dd
, ASIC_GPIO_OUT
) & ~EPROM_WP_N
);
136 write_csr(dd
, ASIC_GPIO_OE
,
137 read_csr(dd
, ASIC_GPIO_OE
) & ~EPROM_WP_N
);
141 * Wait for the device to become not busy. Must be called after all
142 * write or erase operations.
144 static int wait_for_not_busy(struct hfi1_devdata
*dd
)
146 unsigned long count
= 0;
150 /* starts page mode */
151 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_READ_SR1
);
153 udelay(WAIT_SLEEP_US
);
154 usleep_range(WAIT_SLEEP_US
- 5, WAIT_SLEEP_US
+ 5);
156 reg
= read_csr(dd
, ASIC_EEP_DATA
);
157 if ((reg
& SR1_BUSY
) == 0)
159 /* 200s is the largest time for a 128Mb device */
160 if (count
> COUNT_DELAY_SEC(200)) {
161 dd_dev_err(dd
, "waited too long for SPI FLASH busy to clear - failing\n");
163 break; /* break, not goto - must stop page mode */
167 /* stop page mode with a NOP */
168 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_NOP
);
174 * Read the device ID from the SPI controller.
176 static u32
read_device_id(struct hfi1_devdata
*dd
)
178 /* read the Manufacture Device ID */
179 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_READ_MANUF_DEV_ID
);
180 return (u32
)read_csr(dd
, ASIC_EEP_DATA
);
184 * Erase the whole flash.
186 static int erase_chip(struct hfi1_devdata
*dd
)
192 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_WRITE_ENABLE
);
193 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_CHIP_ERASE
);
194 ret
= wait_for_not_busy(dd
);
204 static int erase_range(struct hfi1_devdata
*dd
, u32 start
, u32 len
)
206 u32 end
= start
+ len
;
212 /* check the end points for the minimum erase */
213 if ((start
& MASK_4KB
) || (end
& MASK_4KB
)) {
215 "%s: non-aligned range (0x%x,0x%x) for a 4KB erase\n",
216 __func__
, start
, end
);
222 while (start
< end
) {
223 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_WRITE_ENABLE
);
224 /* check in order of largest to smallest */
225 if (((start
& MASK_64KB
) == 0) && (start
+ SIZE_64KB
<= end
)) {
226 write_csr(dd
, ASIC_EEP_ADDR_CMD
,
227 CMD_SECTOR_ERASE_64KB(start
));
229 } else if (((start
& MASK_32KB
) == 0) &&
230 (start
+ SIZE_32KB
<= end
)) {
231 write_csr(dd
, ASIC_EEP_ADDR_CMD
,
232 CMD_SECTOR_ERASE_32KB(start
));
234 } else { /* 4KB will work */
235 write_csr(dd
, ASIC_EEP_ADDR_CMD
,
236 CMD_SECTOR_ERASE_4KB(start
));
239 ret
= wait_for_not_busy(dd
);
251 * Read a 256 byte (64 dword) EPROM page.
252 * All callers have verified the offset is at a page boundary.
254 static void read_page(struct hfi1_devdata
*dd
, u32 offset
, u32
*result
)
258 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_READ_DATA(offset
));
259 for (i
= 0; i
< EP_PAGE_SIZE
/sizeof(u32
); i
++)
260 result
[i
] = (u32
)read_csr(dd
, ASIC_EEP_DATA
);
261 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_NOP
); /* close open page */
265 * Read length bytes starting at offset. Copy to user address addr.
267 static int read_length(struct hfi1_devdata
*dd
, u32 start
, u32 len
, u64 addr
)
270 u32 buffer
[EP_PAGE_SIZE
/sizeof(u32
)];
273 /* reject anything not on an EPROM page boundary */
274 if ((start
& EEP_PAGE_MASK
) || (len
& EEP_PAGE_MASK
))
277 for (offset
= 0; offset
< len
; offset
+= EP_PAGE_SIZE
) {
278 read_page(dd
, start
+ offset
, buffer
);
279 if (copy_to_user((void __user
*)(addr
+ offset
),
280 buffer
, EP_PAGE_SIZE
)) {
291 * Write a 256 byte (64 dword) EPROM page.
292 * All callers have verified the offset is at a page boundary.
294 static int write_page(struct hfi1_devdata
*dd
, u32 offset
, u32
*data
)
298 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_WRITE_ENABLE
);
299 write_csr(dd
, ASIC_EEP_DATA
, data
[0]);
300 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_PAGE_PROGRAM(offset
));
301 for (i
= 1; i
< EP_PAGE_SIZE
/sizeof(u32
); i
++)
302 write_csr(dd
, ASIC_EEP_DATA
, data
[i
]);
303 /* will close the open page */
304 return wait_for_not_busy(dd
);
308 * Write length bytes starting at offset. Read from user address addr.
310 static int write_length(struct hfi1_devdata
*dd
, u32 start
, u32 len
, u64 addr
)
313 u32 buffer
[EP_PAGE_SIZE
/sizeof(u32
)];
316 /* reject anything not on an EPROM page boundary */
317 if ((start
& EEP_PAGE_MASK
) || (len
& EEP_PAGE_MASK
))
322 for (offset
= 0; offset
< len
; offset
+= EP_PAGE_SIZE
) {
323 if (copy_from_user(buffer
, (void __user
*)(addr
+ offset
),
328 ret
= write_page(dd
, start
+ offset
, buffer
);
338 /* convert an range composite to a length, in bytes */
339 static inline u32
extract_rlen(u32 composite
)
341 return (composite
& 0xffff) * EP_PAGE_SIZE
;
344 /* convert an range composite to a start, in bytes */
345 static inline u32
extract_rstart(u32 composite
)
347 return (composite
>> 16) * EP_PAGE_SIZE
;
351 * Perform the given operation on the EPROM. Called from user space. The
352 * user credentials have already been checked.
354 * Return 0 on success, -ERRNO on error
356 int handle_eprom_command(const struct hfi1_cmd
*cmd
)
358 struct hfi1_devdata
*dd
;
360 u32 rlen
; /* range length */
361 u32 rstart
; /* range start */
365 * The EPROM is per-device, so use unit 0 as that will always
370 pr_err("%s: cannot find unit 0!\n", __func__
);
374 /* lock against other callers touching the ASIC block */
375 mutex_lock(&eprom_mutex
);
377 /* some platforms do not have an EPROM */
378 if (!eprom_available
) {
383 /* lock against the other HFI on another OS */
384 ret
= acquire_hw_mutex(dd
);
387 "%s: unable to acquire hw mutex, no EPROM support\n",
392 dd_dev_info(dd
, "%s: cmd: type %d, len 0x%x, addr 0x%016llx\n",
393 __func__
, cmd
->type
, cmd
->len
, cmd
->addr
);
396 case HFI1_CMD_EP_INFO
:
397 if (cmd
->len
!= sizeof(u32
)) {
401 dev_id
= read_device_id(dd
);
402 /* addr points to a u32 user buffer */
403 if (copy_to_user((void __user
*)cmd
->addr
, &dev_id
,
408 case HFI1_CMD_EP_ERASE_CHIP
:
409 ret
= erase_chip(dd
);
412 case HFI1_CMD_EP_ERASE_RANGE
:
413 rlen
= extract_rlen(cmd
->len
);
414 rstart
= extract_rstart(cmd
->len
);
415 ret
= erase_range(dd
, rstart
, rlen
);
418 case HFI1_CMD_EP_READ_RANGE
:
419 rlen
= extract_rlen(cmd
->len
);
420 rstart
= extract_rstart(cmd
->len
);
421 ret
= read_length(dd
, rstart
, rlen
, cmd
->addr
);
424 case HFI1_CMD_EP_WRITE_RANGE
:
425 rlen
= extract_rlen(cmd
->len
);
426 rstart
= extract_rstart(cmd
->len
);
427 ret
= write_length(dd
, rstart
, rlen
, cmd
->addr
);
431 dd_dev_err(dd
, "%s: unexpected command %d\n",
432 __func__
, cmd
->type
);
437 release_hw_mutex(dd
);
439 mutex_unlock(&eprom_mutex
);
444 * Initialize the EPROM handler.
446 int eprom_init(struct hfi1_devdata
*dd
)
450 /* only the discrete chip has an EPROM, nothing to do */
451 if (dd
->pcidev
->device
!= PCI_DEVICE_ID_INTEL0
)
454 /* lock against other callers */
455 mutex_lock(&eprom_mutex
);
456 if (eprom_available
) /* already initialized */
460 * Lock against the other HFI on another OS - the mutex above
461 * would have caught anything in this driver. It is OK if
462 * both OSes reset the EPROM - as long as they don't do it at
465 ret
= acquire_hw_mutex(dd
);
468 "%s: unable to acquire hw mutex, no EPROM support\n",
473 /* reset EPROM to be sure it is in a good state */
476 write_csr(dd
, ASIC_EEP_CTL_STAT
,
477 ASIC_EEP_CTL_STAT_EP_RESET_SMASK
);
478 /* clear reset, set speed */
479 write_csr(dd
, ASIC_EEP_CTL_STAT
,
480 EP_SPEED_FULL
<< ASIC_EEP_CTL_STAT_RATE_SPI_SHIFT
);
482 /* wake the device with command "release powerdown NoID" */
483 write_csr(dd
, ASIC_EEP_ADDR_CMD
, CMD_RELEASE_POWERDOWN_NOID
);
486 release_hw_mutex(dd
);
488 mutex_unlock(&eprom_mutex
);