2 * Copyright (C) 2011 glevand (geoffrey.levand@mail.ru)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "bootstrap.h"
36 int ps3stor_setup(struct ps3_stordev *sd, int type)
43 err = ps3repo_find_bus_by_type(PS3_BUS_TYPE_STOR, &sd->sd_busidx);
47 err = ps3repo_read_bus_id(sd->sd_busidx, &sd->sd_busid);
51 err = ps3repo_find_bus_dev_by_type(sd->sd_busidx, type, &sd->sd_devidx);
55 err = ps3repo_read_bus_dev_id(sd->sd_busidx, sd->sd_devidx,
60 err = ps3repo_read_bus_dev_blk_size(sd->sd_busidx, sd->sd_devidx,
65 err = ps3repo_read_bus_dev_nblocks(sd->sd_busidx, sd->sd_devidx,
70 err = ps3repo_read_bus_dev_nregs(sd->sd_busidx, sd->sd_devidx,
75 for (i = 0; i < sd->sd_nregs; i++) {
76 err = ps3repo_read_bus_dev_reg_id(sd->sd_busidx, sd->sd_devidx,
77 i, &sd->sd_regs[i].sr_id);
81 err = ps3repo_read_bus_dev_reg_start(sd->sd_busidx,
82 sd->sd_devidx, i, &sd->sd_regs[i].sr_start);
86 err = ps3repo_read_bus_dev_reg_size(sd->sd_busidx,
87 sd->sd_devidx, i, &sd->sd_regs[i].sr_size);
97 err = lv1_open_device(sd->sd_busid, sd->sd_devid, 0);
101 err = lv1_setup_dma(sd->sd_busid, sd->sd_devid, &sd->sd_dmabase);
109 lv1_close_device(sd->sd_busid, sd->sd_devid);
116 static char dma_buf[2048] __aligned(2048);
118 int ps3stor_read_sectors(struct ps3_stordev *sd, int regidx,
119 uint64_t start_sector, uint64_t sector_count, uint64_t flags, char *buf)
121 #define MIN(a, b) ((a) <= (b) ? (a) : (b))
122 #define BOUNCE_SECTORS (sizeof(dma_buf) / sd->sd_blksize)
123 #define ASYNC_STATUS_POLL_PERIOD 100 /* microseconds */
125 struct ps3_storreg *reg = &sd->sd_regs[regidx];
126 uint64_t nleft, nread, nsectors;
127 uint64_t tag, status;
128 unsigned int timeout;
131 nleft = sector_count;
135 nsectors = MIN(nleft, BOUNCE_SECTORS);
137 err = lv1_storage_read(sd->sd_devid, reg->sr_id,
138 start_sector + nread, nsectors, flags, (uint32_t)dma_buf,
143 timeout = 5000000; /* microseconds */
146 if (timeout < ASYNC_STATUS_POLL_PERIOD)
149 err = lv1_storage_check_async_status(sd->sd_devid, tag,
154 delay(ASYNC_STATUS_POLL_PERIOD);
155 timeout -= ASYNC_STATUS_POLL_PERIOD;
161 memcpy(buf + nread * sd->sd_blksize, (u_char *)dma_buf,
162 nsectors * sd->sd_blksize);
170 #undef BOUNCE_SECTORS
171 #undef ASYNC_STATUS_POLL_PERIOD
174 void ps3stor_print(struct ps3_stordev *sd)