tpm_nsc.c (ee1779840d093ebf6893c97115422fb5171b54d7) tpm_nsc.c (9e0d39d8a6a0a8805d05fba22e3fbe80b5c8c4cb)
1/*
2 * Copyright (C) 2004 IBM Corporation
3 *
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>

--- 55 unchanged lines hidden (view full) ---

64 NSC_COMMAND_EOC = 0x03,
65 NSC_COMMAND_CANCEL = 0x22
66};
67
68struct tpm_nsc_priv {
69 unsigned long base;
70};
71
1/*
2 * Copyright (C) 2004 IBM Corporation
3 *
4 * Authors:
5 * Leendert van Doorn <leendert@watson.ibm.com>
6 * Dave Safford <safford@watson.ibm.com>
7 * Reiner Sailer <sailer@watson.ibm.com>
8 * Kylene Hall <kjhall@us.ibm.com>

--- 55 unchanged lines hidden (view full) ---

64 NSC_COMMAND_EOC = 0x03,
65 NSC_COMMAND_CANCEL = 0x22
66};
67
68struct tpm_nsc_priv {
69 unsigned long base;
70};
71
72static inline struct tpm_nsc_priv *nsc_get_priv(struct tpm_chip *chip)
73{
74 return chip->vendor.priv;
75}
76
77/*
78 * Wait for a certain status to appear
79 */
80static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
81{
72/*
73 * Wait for a certain status to appear
74 */
75static int wait_for_stat(struct tpm_chip *chip, u8 mask, u8 val, u8 * data)
76{
77 struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
82 unsigned long stop;
83
84 /* status immediately available check */
78 unsigned long stop;
79
80 /* status immediately available check */
85 *data = inb(nsc_get_priv(chip)->base + NSC_STATUS);
81 *data = inb(priv->base + NSC_STATUS);
86 if ((*data & mask) == val)
87 return 0;
88
89 /* wait for status */
90 stop = jiffies + 10 * HZ;
91 do {
92 msleep(TPM_TIMEOUT);
82 if ((*data & mask) == val)
83 return 0;
84
85 /* wait for status */
86 stop = jiffies + 10 * HZ;
87 do {
88 msleep(TPM_TIMEOUT);
93 *data = inb(nsc_get_priv(chip)->base + 1);
89 *data = inb(priv->base + 1);
94 if ((*data & mask) == val)
95 return 0;
96 }
97 while (time_before(jiffies, stop));
98
99 return -EBUSY;
100}
101
102static int nsc_wait_for_ready(struct tpm_chip *chip)
103{
90 if ((*data & mask) == val)
91 return 0;
92 }
93 while (time_before(jiffies, stop));
94
95 return -EBUSY;
96}
97
98static int nsc_wait_for_ready(struct tpm_chip *chip)
99{
100 struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
104 int status;
105 unsigned long stop;
106
107 /* status immediately available check */
101 int status;
102 unsigned long stop;
103
104 /* status immediately available check */
108 status = inb(nsc_get_priv(chip)->base + NSC_STATUS);
105 status = inb(priv->base + NSC_STATUS);
109 if (status & NSC_STATUS_OBF)
106 if (status & NSC_STATUS_OBF)
110 status = inb(nsc_get_priv(chip)->base + NSC_DATA);
107 status = inb(priv->base + NSC_DATA);
111 if (status & NSC_STATUS_RDY)
112 return 0;
113
114 /* wait for status */
115 stop = jiffies + 100;
116 do {
117 msleep(TPM_TIMEOUT);
108 if (status & NSC_STATUS_RDY)
109 return 0;
110
111 /* wait for status */
112 stop = jiffies + 100;
113 do {
114 msleep(TPM_TIMEOUT);
118 status = inb(nsc_get_priv(chip)->base + NSC_STATUS);
115 status = inb(priv->base + NSC_STATUS);
119 if (status & NSC_STATUS_OBF)
116 if (status & NSC_STATUS_OBF)
120 status = inb(nsc_get_priv(chip)->base + NSC_DATA);
117 status = inb(priv->base + NSC_DATA);
121 if (status & NSC_STATUS_RDY)
122 return 0;
123 }
124 while (time_before(jiffies, stop));
125
126 dev_info(&chip->dev, "wait for ready failed\n");
127 return -EBUSY;
128}
129
130
131static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
132{
118 if (status & NSC_STATUS_RDY)
119 return 0;
120 }
121 while (time_before(jiffies, stop));
122
123 dev_info(&chip->dev, "wait for ready failed\n");
124 return -EBUSY;
125}
126
127
128static int tpm_nsc_recv(struct tpm_chip *chip, u8 * buf, size_t count)
129{
130 struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
133 u8 *buffer = buf;
134 u8 data, *p;
135 u32 size;
136 __be32 *native_size;
137
138 if (count < 6)
139 return -EIO;
140
141 if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
142 dev_err(&chip->dev, "F0 timeout\n");
143 return -EIO;
144 }
145
131 u8 *buffer = buf;
132 u8 data, *p;
133 u32 size;
134 __be32 *native_size;
135
136 if (count < 6)
137 return -EIO;
138
139 if (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0) {
140 dev_err(&chip->dev, "F0 timeout\n");
141 return -EIO;
142 }
143
146 data = inb(nsc_get_priv(chip)->base + NSC_DATA);
144 data = inb(priv->base + NSC_DATA);
147 if (data != NSC_COMMAND_NORMAL) {
148 dev_err(&chip->dev, "not in normal mode (0x%x)\n",
149 data);
150 return -EIO;
151 }
152
153 /* read the whole packet */
154 for (p = buffer; p < &buffer[count]; p++) {
155 if (wait_for_stat
156 (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
157 dev_err(&chip->dev,
158 "OBF timeout (while reading data)\n");
159 return -EIO;
160 }
161 if (data & NSC_STATUS_F0)
162 break;
145 if (data != NSC_COMMAND_NORMAL) {
146 dev_err(&chip->dev, "not in normal mode (0x%x)\n",
147 data);
148 return -EIO;
149 }
150
151 /* read the whole packet */
152 for (p = buffer; p < &buffer[count]; p++) {
153 if (wait_for_stat
154 (chip, NSC_STATUS_OBF, NSC_STATUS_OBF, &data) < 0) {
155 dev_err(&chip->dev,
156 "OBF timeout (while reading data)\n");
157 return -EIO;
158 }
159 if (data & NSC_STATUS_F0)
160 break;
163 *p = inb(nsc_get_priv(chip)->base + NSC_DATA);
161 *p = inb(priv->base + NSC_DATA);
164 }
165
166 if ((data & NSC_STATUS_F0) == 0 &&
167 (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0)) {
168 dev_err(&chip->dev, "F0 not set\n");
169 return -EIO;
170 }
171
162 }
163
164 if ((data & NSC_STATUS_F0) == 0 &&
165 (wait_for_stat(chip, NSC_STATUS_F0, NSC_STATUS_F0, &data) < 0)) {
166 dev_err(&chip->dev, "F0 not set\n");
167 return -EIO;
168 }
169
172 data = inb(nsc_get_priv(chip)->base + NSC_DATA);
170 data = inb(priv->base + NSC_DATA);
173 if (data != NSC_COMMAND_EOC) {
174 dev_err(&chip->dev,
175 "expected end of command(0x%x)\n", data);
176 return -EIO;
177 }
178
179 native_size = (__force __be32 *) (buf + 2);
180 size = be32_to_cpu(*native_size);
181
182 if (count < size)
183 return -EIO;
184
185 return size;
186}
187
188static int tpm_nsc_send(struct tpm_chip *chip, u8 * buf, size_t count)
189{
171 if (data != NSC_COMMAND_EOC) {
172 dev_err(&chip->dev,
173 "expected end of command(0x%x)\n", data);
174 return -EIO;
175 }
176
177 native_size = (__force __be32 *) (buf + 2);
178 size = be32_to_cpu(*native_size);
179
180 if (count < size)
181 return -EIO;
182
183 return size;
184}
185
186static int tpm_nsc_send(struct tpm_chip *chip, u8 * buf, size_t count)
187{
188 struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
190 u8 data;
191 int i;
192
193 /*
194 * If we hit the chip with back to back commands it locks up
195 * and never set IBF. Hitting it with this "hammer" seems to
196 * fix it. Not sure why this is needed, we followed the flow
197 * chart in the manual to the letter.
198 */
189 u8 data;
190 int i;
191
192 /*
193 * If we hit the chip with back to back commands it locks up
194 * and never set IBF. Hitting it with this "hammer" seems to
195 * fix it. Not sure why this is needed, we followed the flow
196 * chart in the manual to the letter.
197 */
199 outb(NSC_COMMAND_CANCEL, nsc_get_priv(chip)->base + NSC_COMMAND);
198 outb(NSC_COMMAND_CANCEL, priv->base + NSC_COMMAND);
200
201 if (nsc_wait_for_ready(chip) != 0)
202 return -EIO;
203
204 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
205 dev_err(&chip->dev, "IBF timeout\n");
206 return -EIO;
207 }
208
199
200 if (nsc_wait_for_ready(chip) != 0)
201 return -EIO;
202
203 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
204 dev_err(&chip->dev, "IBF timeout\n");
205 return -EIO;
206 }
207
209 outb(NSC_COMMAND_NORMAL, nsc_get_priv(chip)->base + NSC_COMMAND);
208 outb(NSC_COMMAND_NORMAL, priv->base + NSC_COMMAND);
210 if (wait_for_stat(chip, NSC_STATUS_IBR, NSC_STATUS_IBR, &data) < 0) {
211 dev_err(&chip->dev, "IBR timeout\n");
212 return -EIO;
213 }
214
215 for (i = 0; i < count; i++) {
216 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
217 dev_err(&chip->dev,
218 "IBF timeout (while writing data)\n");
219 return -EIO;
220 }
209 if (wait_for_stat(chip, NSC_STATUS_IBR, NSC_STATUS_IBR, &data) < 0) {
210 dev_err(&chip->dev, "IBR timeout\n");
211 return -EIO;
212 }
213
214 for (i = 0; i < count; i++) {
215 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
216 dev_err(&chip->dev,
217 "IBF timeout (while writing data)\n");
218 return -EIO;
219 }
221 outb(buf[i], nsc_get_priv(chip)->base + NSC_DATA);
220 outb(buf[i], priv->base + NSC_DATA);
222 }
223
224 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
225 dev_err(&chip->dev, "IBF timeout\n");
226 return -EIO;
227 }
221 }
222
223 if (wait_for_stat(chip, NSC_STATUS_IBF, 0, &data) < 0) {
224 dev_err(&chip->dev, "IBF timeout\n");
225 return -EIO;
226 }
228 outb(NSC_COMMAND_EOC, nsc_get_priv(chip)->base + NSC_COMMAND);
227 outb(NSC_COMMAND_EOC, priv->base + NSC_COMMAND);
229
230 return count;
231}
232
233static void tpm_nsc_cancel(struct tpm_chip *chip)
234{
228
229 return count;
230}
231
232static void tpm_nsc_cancel(struct tpm_chip *chip)
233{
235 outb(NSC_COMMAND_CANCEL, nsc_get_priv(chip)->base + NSC_COMMAND);
234 struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
235
236 outb(NSC_COMMAND_CANCEL, priv->base + NSC_COMMAND);
236}
237
238static u8 tpm_nsc_status(struct tpm_chip *chip)
239{
237}
238
239static u8 tpm_nsc_status(struct tpm_chip *chip)
240{
240 return inb(nsc_get_priv(chip)->base + NSC_STATUS);
241 struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
242
243 return inb(priv->base + NSC_STATUS);
241}
242
243static bool tpm_nsc_req_canceled(struct tpm_chip *chip, u8 status)
244{
245 return (status == NSC_STATUS_RDY);
246}
247
248static const struct tpm_class_ops tpm_nsc = {

--- 6 unchanged lines hidden (view full) ---

255 .req_canceled = tpm_nsc_req_canceled,
256};
257
258static struct platform_device *pdev = NULL;
259
260static void tpm_nsc_remove(struct device *dev)
261{
262 struct tpm_chip *chip = dev_get_drvdata(dev);
244}
245
246static bool tpm_nsc_req_canceled(struct tpm_chip *chip, u8 status)
247{
248 return (status == NSC_STATUS_RDY);
249}
250
251static const struct tpm_class_ops tpm_nsc = {

--- 6 unchanged lines hidden (view full) ---

258 .req_canceled = tpm_nsc_req_canceled,
259};
260
261static struct platform_device *pdev = NULL;
262
263static void tpm_nsc_remove(struct device *dev)
264{
265 struct tpm_chip *chip = dev_get_drvdata(dev);
266 struct tpm_nsc_priv *priv = dev_get_drvdata(&chip->dev);
263
264 tpm_chip_unregister(chip);
267
268 tpm_chip_unregister(chip);
265 release_region(nsc_get_priv(chip)->base, 2);
269 release_region(priv->base, 2);
266}
267
268static SIMPLE_DEV_PM_OPS(tpm_nsc_pm, tpm_pm_suspend, tpm_pm_resume);
269
270static struct platform_driver nsc_drv = {
271 .driver = {
272 .name = "tpm_nsc",
273 .pm = &tpm_nsc_pm,

--- 55 unchanged lines hidden (view full) ---

329 }
330
331 chip = tpmm_chip_alloc(&pdev->dev, &tpm_nsc);
332 if (IS_ERR(chip)) {
333 rc = -ENODEV;
334 goto err_rel_reg;
335 }
336
270}
271
272static SIMPLE_DEV_PM_OPS(tpm_nsc_pm, tpm_pm_suspend, tpm_pm_resume);
273
274static struct platform_driver nsc_drv = {
275 .driver = {
276 .name = "tpm_nsc",
277 .pm = &tpm_nsc_pm,

--- 55 unchanged lines hidden (view full) ---

333 }
334
335 chip = tpmm_chip_alloc(&pdev->dev, &tpm_nsc);
336 if (IS_ERR(chip)) {
337 rc = -ENODEV;
338 goto err_rel_reg;
339 }
340
337 chip->vendor.priv = priv;
341 dev_set_drvdata(&chip->dev, priv);
338
339 rc = tpm_chip_register(chip);
340 if (rc)
341 goto err_rel_reg;
342
343 dev_dbg(&pdev->dev, "NSC TPM detected\n");
344 dev_dbg(&pdev->dev,
345 "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",

--- 60 unchanged lines hidden ---
342
343 rc = tpm_chip_register(chip);
344 if (rc)
345 goto err_rel_reg;
346
347 dev_dbg(&pdev->dev, "NSC TPM detected\n");
348 dev_dbg(&pdev->dev,
349 "NSC LDN 0x%x, SID 0x%x, SRID 0x%x\n",

--- 60 unchanged lines hidden ---